Skip to content

Commit

Permalink
Port /say, /avatar and /remindme to CommandClient.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed May 13, 2018
1 parent 3bef6fe commit 792071d
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 74 deletions.
74 changes: 71 additions & 3 deletions server/bot/commands/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// All the types!
import { Message } from 'eris' // eslint-disable-line no-unused-vars
import { IveBotCommand } from '../imports/types'
// All the needs!
import { getIdFromMention, getDesc } from '../imports/tools'
import * as ms from 'ms'
import 'json5/lib/require'
import { host, testPilots } from '../../../config.json5'

export const handleRequest: IveBotCommand = {
export const handleRequest: IveBotCommand = (client, db) => ({
name: 'request',
opts: {
aliases: ['req'],
Expand All @@ -13,7 +16,7 @@ export const handleRequest: IveBotCommand = {
fullDescription: 'Request a feature. Only available to test pilots.',
usage: '/request <suggestion>'
},
generator: (client) => ({ author, content, channel }, args) => {
generator: ({ author, content, channel }, args) => {
client.getDMChannel(host).then((PrivateChannel) => {
client.createMessage(
PrivateChannel.id,
Expand All @@ -25,4 +28,69 @@ and will be read shortly.
You may recieve a response soon, and you can keep track here:
<https://github.com/retrixe/IveBot/projects/1>`
}
}
})

export const handleSay: IveBotCommand = (client, db) => ({
opts: {
requirements: { userIDs: [...testPilots, host], permissions: { manageMessages: true } },
permissionMessage: 'You cannot fool me. You do not have enough permissions.',
description: 'Say something, even in another channel.',
fullDescription: 'Say something. Test pilots and admins/mods only.',
usage: '/say (channel) <text>',
deleteCommand: true,
errorMessage: 'There was an error processing your request.'
},
name: 'say',
generator: (message, args) => {
// Should it be sent in another channel?
const possibleChannel = getIdFromMention(args[0])
if (message.channelMentions[0] === possibleChannel) {
args.shift()
client.createMessage(message.channelMentions[0], args.join(' ')).then((newMessage) => {
db.say[message.channelMentions[0]] = newMessage.id
}).catch((err) => console.error(err))
return
}
// Send the message.
client.createMessage(message.channel.id, args.join(' ')).then((newMessage) => {
db.say[message.channelMentions[0]] = newMessage.id
}).catch((err) => console.error(err))
}
})

export const handleRemindme: IveBotCommand = (client) => ({
opts: {
fullDescription: 'Remind you of something.',
description: 'Reminders.',
usage: '/remindme <time in 1d|1h|1m|1s> <description>',
aliases: ['rm']
},
name: 'remindme',
generator: (message, args) => {
if (args.length < 2 || !ms(args[0])) {
return 'Correct usage: /remindme <time in 1d|1h|1m|1s> <description>'
}
client.createMessage(message.channel.id, `You will be reminded in ${args[0]} through a DM.`)
setTimeout(() => {
client.getDMChannel(message.author.id).then((PrivateChannel) => client.createMessage(
PrivateChannel.id, `⏰ ${getDesc(message)}\nReminder set ${args[0]} ago.`
))
}, ms(args[0]))
}
})

export const handleAvatar: IveBotCommand = (client) => ({
opts: {
fullDescription: 'Get a large-sized link to the avatar of a user.',
description: 'Avatar of a user.',
usage: '/avatar <user>',
aliases: ['av'],
argsRequired: false
},
name: 'avatar',
generator: (message, args) => {
let user: Message['author'] = message.author
if (message.mentions.length !== 0) user = message.mentions[0]
return 'Link: ' + user.avatarURL.split('128').join('') + '2048'
}
})
2 changes: 2 additions & 0 deletions server/bot/imports/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const getArguments = (message: string) => {
return splitMessage.join(' ').trim()
}

export const getDesc = (message: { content: string }) => getArguments(message.content)

export const getIdFromMention = (mention: string) => {
const f = mention.substring(2, mention.length - 1).split('!').join('').split('&').join('').split(':')
return f[f.length - 1]
Expand Down
6 changes: 3 additions & 3 deletions server/bot/imports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
/* eslint-disable no-undef */
import { CommandClient, Member, Message, CommandOptions, CommandGenerator } from 'eris'

export type IveBotCommand = {
// eslint-disable-next-line no-use-before-define
generator: (client: CommandClient) => CommandGenerator,
// eslint-disable-next-line no-use-before-define
export type IveBotCommand = (client: CommandClient, db?: DB) => {
generator: CommandGenerator,
opts: CommandOptions,
name: string
}
Expand Down
11 changes: 2 additions & 9 deletions server/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { randomBytes } from 'crypto'
import * as ms from 'ms'
// Commands.
import {
handleSay, handleEditLastSay, handleAvatar, handleListserverregions,
handleEdit, handleType, handleRemindme, handleChangeserverregion
handleEditLastSay, handleListserverregions,
handleEdit, handleType, handleChangeserverregion
} from './oldCommands/utilities'
import {
handleChoose,
Expand Down Expand Up @@ -196,17 +196,10 @@ export default (client: client, tempDB: DB) => async (event: message) => {
'/gfi': () => handleGunfight(message, client.user.id, userID, sendResponse, tempDB, channelID),
'/accept': () => handleAccept(tempDB, userID, sendResponse, channelID),
// Say.
'/say': () => handleSay(event, sendResponse, client, testPilot, tempDB),
'/type': () => handleType(event, sendResponse, client, testPilot, tempDB),
'/editLastSay': () => handleEditLastSay(event, sendResponse, client, testPilot, tempDB),
'/els': () => handleEditLastSay(event, sendResponse, client, testPilot, tempDB),
'/edit': () => handleEdit(event, sendResponse, client),
// Avatar.
'/avatar': () => handleAvatar(message, sendResponse, client, userID),
'/av': () => handleAvatar(message, sendResponse, client, userID),
// Remind me.
'/remindme': () => handleRemindme(message, sendResponse, client, userID),
'/rm': () => handleRemindme(message, sendResponse, client, userID),
// Server region commands.
'/changeserverregion': () => handleChangeserverregion(client, event, sendResponse, message),
'/csr': () => handleChangeserverregion(client, event, sendResponse, message),
Expand Down
3 changes: 2 additions & 1 deletion server/bot/oldCommands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ let generalHelp = ` ** Jony Ive can do many commands 📡**
TP \`/request\` - Request a specific feature.
\`/link\` - Links your Discord to IveBot Web (use in DM only)
\`/weather\` - It's really cloudy here..
\`/remindme\` - Reminders.
\`/say\` | \`/type\` - Say something, even in another channel.
\`/editLastSay\` - Even if it was another channel.
\`/avatar\` - Avatar of a user.
Expand Down Expand Up @@ -98,7 +99,7 @@ const commandDocs: { [index: string]: any } = {
'type': b('/type (channel) <text>', 'Type something. Test pilots and admins/mods only.', '/type #general heyo'),
'editLastSay': b('/editLastSay (channel) <new text>', 'Edits the last say in a channel.', '/editLastSay #general hey', '/els'),
'edit': b('/edit (channel) <message ID> <new text>', 'Edits a single message. Owner only command.', '/edit #general 123456789012345678 hi'),
'avatar': b('/avatar <user>', 'Avatar of a user.', '/avatar @voldemort#6931', '/av'),
'avatar': b('/avatar <user>', 'Get a large-sized link to the avatar of a user.', '/avatar @voldemort#6931', '/av'),
'remindme': b('/remindme <time in 1d|1h|1m|1s> <description>', 'Remind you of something.', '/remindme 1h do your homework', '/rm'),
'about': b('/about', 'About IveBot.', '/about'),
'ping': b('/ping', 'Latency of IveBot\'s connection to your server.', '/ping'),
Expand Down
56 changes: 0 additions & 56 deletions server/bot/oldCommands/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@ import { getArguments, getIdFromMention } from '../imports/tools'
import { client, message, DB } from '../imports/types'
import 'json5/lib/require'
import { host } from '../../../config.json5'
import * as ms from 'ms'

export function handleSay (message: message, sendResponse: Function, client: client, testPilot: string, db: DB) {
// Check for enough permissions.
let check = false
if (message.member.permission.has('manageMessages')) check = true
else if (testPilot) check = true
if (!check) {
sendResponse('You cannot fool me. You do not have enough permissions.')
return
}
// Delete the message.
client.deleteMessage(message.channel.id, message.id)
// Should it be edited in another channel?
const possibleChannel = getIdFromMention(getArguments(message.content).split(' ')[0])
if (message.channelMentions[0] === possibleChannel) {
client.createMessage(message.channelMentions[0], getArguments(getArguments(message.content)))
.then((newMessage) => { db.say[message.channelMentions[0]] = newMessage.id })
.catch(() => sendResponse('There was an error processing your request.'))
return
}
// Send the message all over again.
sendResponse(getArguments(message.content))
.then((newMessage: { id: string }) => { db.say[message.channel.id] = newMessage.id })
.catch(() => sendResponse('There was an error processing your request.'))
}

export function handleEditLastSay (message: message, sendResponse: Function, client: client, testPilot: string, db: DB) {
// Check for enough permissions.
Expand Down Expand Up @@ -113,36 +87,6 @@ export function handleEdit (message: message, sendResponse: Function, client: cl
).catch(() => sendResponse('Nothing to edit.'))
}

export function handleAvatar (message: string, sendResponse: Function, client: client, userID: string) {
let user: string = getIdFromMention(getArguments(message).split(' ')[0])
if (!user) user = userID
if (!client.users.find(userElement => userElement.id === user).avatarURL) {
sendResponse(
'Link: https://cdn.discordapp.com/embed/avatars/' +
+client.users.find(userElement => userElement.id === user).discriminator % 5 +
'.png?size=2048'
)
return
}
sendResponse('Link: ' + client.users.find(E => E.id === user).avatarURL + '?size=2048')
}

export function handleRemindme (
message: string, sendResponse: Function, client: client, userID: string
) {
if (message.split(' ').length < 3 || !ms(message.split(' ')[1])) {
sendResponse('Correct usage: /remindme <time in 1d|1h|1m|1s> <description>')
return
}
const description = getArguments(getArguments(message))
sendResponse(`You will be reminded in ${message.split(' ')[1]} - ${description} - through a DM.`)
setTimeout(() => {
client.getDMChannel(userID).then((PrivateChannel) => client.createMessage(
PrivateChannel.id, `⏰ ${description}\nReminder set ${message.split(' ')[1]} ago.`
))
}, ms(message.split(' ')[1]))
}

// List server regions!
export function handleListserverregions (client: client, event: message, message: string, sendResponse: Function) {
client.getVoiceRegions(event.member.guild.id).then((listOfServer) => {
Expand Down
4 changes: 2 additions & 2 deletions server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ readdir('./server/bot/commands', (err, commandFiles) => {
if (!Object.keys(commands).length) return
// ..register the commands.
Object.keys(commands).forEach((commandName: string) => {
const command = commands[commandName]
client.registerCommand(command.name, command.generator(client), command.opts)
const command = commands[commandName](client, tempDB)
client.registerCommand(command.name, command.generator, command.opts)
})
}
})
Expand Down

0 comments on commit 792071d

Please sign in to comment.