Skip to content

Commit

Permalink
Move further logic into the Command class and add /eval.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Oct 11, 2018
1 parent 8f7fb6d commit 18dd0f4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
17 changes: 9 additions & 8 deletions server/bot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ export class Command {
}

async execute (context: Context, message: Message, args: string[]) { // eslint-disable-line indent
if (!this.requirementsCheck(message)) {
// We check for arguments.
if (args.length === 0 && this.argsRequired) {
message.channel.createMessage(this.invalidUsageMessage)
return
// Guild and DM only.
} else if (this.guildOnly && message.channel.type !== 0) return
else if (this.dmOnly && message.channel.type !== 1) return
// Check for permissions.
else if (!this.requirementsCheck(message)) {
message.channel.createMessage(
`**Thankfully, you don't have enough permissions for that, you ${getInsult()}.**`
)
Expand Down Expand Up @@ -137,13 +145,6 @@ export default class CommandParser {
}
const args = message.content.split(' ')
args.shift()
// We check for arguments.
if (args.length === 0 && command.argsRequired) {
message.channel.createMessage(command.invalidUsageMessage)
return
// Guild and DM only.
} else if (command.guildOnly && message.channel.type !== 0) return
else if (command.dmOnly && message.channel.type !== 1) return
// We get the exact content to send.
const messageToSend = await command.execute(context, message, args)
// We define a sent variable to keep track.
Expand Down
27 changes: 27 additions & 0 deletions server/bot/commands/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,30 @@ export const handlePing: Command = {
sent.edit(`Aha! IveBot ${version} is connected to your server with a ${e}`)
}
}

export const handleEval: Command = {
name: 'eval',
opts: {
description: 'Runs JavaScript. Owner only.',
fullDescription: 'Runs JavaScript. Owner only.',
usage: '/eval <code in codeblock or not>',
example: '/eval ```js\nconsole.log(\'ji\')\n```',
requirements: { userIDs: [host] }
},
generator: async (message, args, context) => {
try {
let toEval = args.join(' ')
if (toEval.startsWith('```js')) toEval = toEval.substring(5)
if (toEval.endsWith('```')) toEval = toEval.substring(0, toEval.length - 3)
// eslint-disable-next-line no-eval
const res = eval(toEval.split('```').join(''))
message.addReaction('✅')
return res || undefined
} catch (e) {
const channel = await context.client.getDMChannel(host)
message.addReaction('❌')
channel.createMessage(`**Error:**
${e}`)
}
}
}

0 comments on commit 18dd0f4

Please sign in to comment.