Skip to content

Commit

Permalink
Enhance eval and uptime, fix some bugs which may crash IveBot.
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Dec 4, 2018
1 parent a888294 commit b861954
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/bot/commands/gunfight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const handleGunfight: Command = {
message.channel.id, `${user.mention}, say /accept to accept the challenge.`
)
// The following will delete the gunfight if unaccepted within 30 seconds.
setTimeout(() => {
setTimeout(async () => {
// Find the gunfight we pushed.
const gunfightPushed = tempDB.gunfight.find((gunfight) => gunfight.challenged === user.id)
// Remove the object and send a response.
Expand Down
34 changes: 22 additions & 12 deletions server/bot/commands/tools.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Command } from '../imports/types'
import { randomBytes } from 'crypto'
import * as ms from 'ms'
import { version } from '../../../package.json'
import { execSync } from 'child_process'
import * as moment from 'moment'
import 'json5/lib/require'
import { host, testPilots } from '../../../config.json5'
import { runInNewContext } from 'vm'
Expand Down Expand Up @@ -30,7 +30,7 @@ export const handleToken: Command = {
'**DO NOT SHARE THIS WITH ANYONE >_<** | Your token is:'
)
const tokenMessage = await dm.createMessage(`**${secureToken}**`)
setTimeout(() => { dmMessage.delete(); tokenMessage.delete() }, 30000)
setTimeout(async () => { dmMessage.delete(); tokenMessage.delete() }, 30000)
} catch (e) { return 'There was an error processing your request (unable to DM token)' }
} catch (e) { return 'There was an error processing your request (unable to DM)' }
// The non-DM part.
Expand Down Expand Up @@ -80,7 +80,10 @@ export const handleUptime: Command = {
example: '/uptime',
argsRequired: false
},
generator: (message, args, { client }) => ms(client.uptime, { long: true })
generator: (message, args, { client }) => {
const d = moment.duration(client.uptime)
return `${d.days()} days ${d.hours()} hours ${d.minutes()} minutes ${d.seconds()} seconds`
}
}

export const handleRemoteexec: Command = {
Expand Down Expand Up @@ -134,12 +137,16 @@ export const handleEval: Command = {
generator: async (message, args, { client, tempDB, db, commandParser }) => {
try {
let toEval = args.join(' ')
if (toEval.startsWith('```js')) toEval = toEval.substring(5)
if (toEval.endsWith('```')) toEval = toEval.substring(0, toEval.length - 3)
// Remove extra characters.
if (toEval.startsWith('`')) toEval = toEval.substring(1)
if (toEval.startsWith('``js')) toEval = toEval.substring(4)
if (toEval.endsWith('`')) toEval = toEval.substring(0, toEval.length - 1)
if (toEval.endsWith('```')) toEval = toEval.substring(0, toEval.length - 2)
// eslint-disable-next-line no-eval
const res = eval(toEval.split('```').join(''))
const res = await eval(toEval)
// const res = eval(`(async () => { const a = ${toEval}; message.channel.createMessage(a) })()`)
message.addReaction('✅')
return res || undefined
return res ? `${'```'}${res}${'```'}` : undefined
} catch (e) {
const channel = await client.getDMChannel(host)
message.addReaction('❌')
Expand All @@ -162,18 +169,21 @@ export const handleSafeeval: Command = {
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)
// Remove extra characters.
if (toEval.startsWith('`')) toEval = toEval.substring(1)
if (toEval.startsWith('``js')) toEval = toEval.substring(4)
if (toEval.endsWith('`')) toEval = toEval.substring(0, toEval.length - 1)
if (toEval.endsWith('```')) toEval = toEval.substring(0, toEval.length - 2)
const res = runInNewContext(toEval.split('```').join(''), {
createMessage: (co: string) => context.client.createMessage(message.channel.id, co),
content: message.content
})
message.addReaction('✅')
return res ? res.toString() : undefined
return res ? `${'```'}${res}${'```'}` : undefined
} catch (e) {
message.addReaction('❌')
message.channel.createMessage(`**Error:**
${e}`)
return `**Error:**
${e}`
}
}
}
2 changes: 1 addition & 1 deletion server/bot/commands/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export const handleLeave: Command = {
'You will require an invite link to join back. Type /leave to confirm.'
)
tempDB.leave.push(message.author.id)
setTimeout(() => {
setTimeout(async () => {
if (tempDB.leave.findIndex(i => i === message.author.id) === -1) return
client.createMessage(message.channel.id, 'Your leave request has timed out.')
tempDB.leave.splice(tempDB.leave.findIndex(i => i === message.author.id), 1)
Expand Down
8 changes: 4 additions & 4 deletions server/bot/imports/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export const getUser = (message: Message, arg: string) => {
const guild = message.member.guild
if (guild.members.find(i => i.id === arg)) return guild.members.find(i => i.id === arg).user
else if (mentions.length && mentions[0].id === getIdFromMention(arg)) return mentions[0]
else if (guild.members.find(i => i.username.toLowerCase() === arg.toLowerCase())) {
return guild.members.find(i => i.username.toLowerCase() === arg.toLowerCase()).user
else if (guild.members.find(i => i && i.username.toLowerCase() === arg.toLowerCase())) {
return guild.members.find(i => i && i.username.toLowerCase() === arg.toLowerCase()).user
} else if (guild.members.find(
i => i.username.toLowerCase() + '#' + i.discriminator === arg.toLowerCase()
i => i && i.username.toLowerCase() + '#' + i.discriminator === arg.toLowerCase()
)) {
return guild.members.find(
i => i.username.toLowerCase() + '#' + i.discriminator === arg.toLowerCase()
i => i && i.username.toLowerCase() + '#' + i.discriminator === arg.toLowerCase()
).user
}
}
Expand Down

0 comments on commit b861954

Please sign in to comment.