Skip to content

Commit

Permalink
Fix bot callback, port more commands. Update architecture.
Browse files Browse the repository at this point in the history
I think we should update the architecture and remove...
...curried functions from the equation.
  • Loading branch information
retrixe committed Sep 8, 2018
1 parent b9ac400 commit 34beee2
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 325 deletions.
29 changes: 20 additions & 9 deletions server/bot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ function isEquivalent (a: { [index: string]: boolean }, b: { [index: string]: bo
export class Command {
name: string // eslint-disable-next-line no-undef
aliases: string[] // eslint-disable-next-line no-undef
generators: (client: Client, db?: DB, mongoDB?: Db) => ({ // eslint-disable-next-line no-undef
generator: IveBotCommandGenerator,
// eslint-disable-next-line no-undef
postGenerator?: (message: Message, args: string[], sent?: Message) => void
})
// eslint-disable-next-line no-undef
generator: (client: Client, db?: DB, mongoDB?: Db) => IveBotCommandGenerator
// eslint-disable-next-line no-undef
postGenerator?: (client: Client, db?: DB, mongoDB?: Db) => (
message: Message, args: string[], sent?: Message // eslint-disable-line no-undef
) => void
argsRequired: boolean // eslint-disable-line no-undef
caseInsensitive: boolean // eslint-disable-line no-undef
deleteCommand: boolean // eslint-disable-line no-undef
Expand All @@ -42,6 +43,7 @@ export class Command {
fullDescription: string // eslint-disable-line no-undef
usage: string // eslint-disable-line no-undef
example: string // eslint-disable-line no-undef
invalidUsageMessage: string // eslint-disable-line no-undef
hidden: boolean // eslint-disable-line no-undef
// eslint-disable-next-line no-undef
requirements: { // eslint-disable-next-line no-undef
Expand All @@ -56,9 +58,11 @@ export class Command {
// Key functions.
this.name = command.name
this.aliases = command.aliases
this.generators = command.generators
this.generator = command.generator
this.postGenerator = command.postGenerator
// Options.
this.argsRequired = command.opts.argsRequired === undefined || command.opts.argsRequired
this.invalidUsageMessage = command.opts.invalidUsageMessage || 'Invalid usage.'
// No impl for next.
this.caseInsensitive = command.opts.caseInsensitive === undefined || command.opts.caseInsensitive
this.deleteCommand = command.opts.deleteCommand
Expand Down Expand Up @@ -133,7 +137,11 @@ export default class CommandParser {

async executeCommand (command: Command, message: Message) {
// We give our generators what they need.
const session = command.generators(this.client, this.tempDB, this.db)
const session = {
generator: command.generator(this.client, this.tempDB, this.db),
postGenerator: command.postGenerator
? command.postGenerator(this.client, this.tempDB, this.db) : undefined
}
const args = message.content.split(' ')
args.shift()
// We check for requirements and arguments.
Expand All @@ -143,7 +151,7 @@ export default class CommandParser {
)
return
} else if (args.length === 0 && command.argsRequired) {
message.channel.createMessage('Invalid usage.')
message.channel.createMessage(command.invalidUsageMessage)
return
// Guild and DM only.
} else if (command.guildOnly && message.channel.type !== 0) return
Expand All @@ -162,7 +170,10 @@ export default class CommandParser {
}

onMessage (message: Message) {
if (!message.content.split(' ')[0].startsWith('/')) return // Don't process it if it's not a command.
if (!message.content.split(' ')[0].startsWith('/')) {
botCallback(message, this.client, this.tempDB, this.db)
return // Don't process it if it's not a command.
}
const commandExec = message.content.split(' ')[0].substr(1).toLowerCase()
// Webhook and bot protection.
try { if (message.author.bot) return } catch (e) { return }
Expand Down
88 changes: 42 additions & 46 deletions server/bot/commands/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,22 @@ export const handlePurge: Command = {
)
}
},
generators: (client) => ({
generator: async (message, args) => {
// Check if usage is correct.
if (
isNaN(+args[0]) || args.length !== 1 || +args[0] <= 0 || +args[0] > 100
) { return 'Correct usage: /purge <number greater than 0 and less than 100>' }
// Pre-defined variables.
let messages: Array<Message>
// Get the list of messages.
try {
messages = await client.getMessages(message.channel.id, +args.shift(), message.id)
} catch (e) { return 'Could not retrieve messages.' }
// Delete the messages.
try {
client.deleteMessages(message.channel.id, messages.map(i => i.id), args.join(' ') || 'Purge')
} catch (e) { return 'Could not delete messages. Are the messages older than 2 weeks?' }
}
})
generator: (client) => async (message, args) => {
// Check if usage is correct.
if (
isNaN(+args[0]) || args.length !== 1 || +args[0] <= 0 || +args[0] > 100
) { return 'Correct usage: /purge <number greater than 0 and less than 100>' }
// Pre-defined variables.
let messages: Array<Message>
// Get the list of messages.
try {
messages = await client.getMessages(message.channel.id, +args.shift(), message.id)
} catch (e) { return 'Could not retrieve messages.' }
// Delete the messages.
try {
client.deleteMessages(message.channel.id, messages.map(i => i.id), args.join(' ') || 'Purge')
} catch (e) { return 'Could not delete messages. Are the messages older than 2 weeks?' }
}
}

export const handleKick: Command = {
Expand All @@ -54,34 +52,32 @@ export const handleKick: Command = {
example: '/kick voldemort you is suck',
requirements: { permissions: { 'kickMembers': true } }
},
generators: (client) => ({
generator: async (message, args) => {
// Find the user ID.
let user = getUser(message, args.shift())
if (!user) return `Specify a valid member of this guild, ${getInsult()}.`
// If the user cannot kick the person..
if (
checkRolePosition(message.member.guild.members.find(i => i.user === user)) >=
checkRolePosition(message.member)
) {
return `You cannot kick this person, you ${getInsult()}.`
}
// Now we kick the person.
try {
await client.kickGuildMember(message.member.guild.id, user.id, args.join(' '))
} catch (e) { return 'I am unable to kick that user.' }
client.createMessage((await client.getDMChannel(user.id)).id, args.length !== 0
? `You have been kicked from ${message.member.guild.name} for ${args.join(' ')}.`
: `You have been kicked from ${message.member.guild.name}.`
generator: (client) => async (message, args) => {
// Find the user ID.
let user = getUser(message, args.shift())
if (!user) return `Specify a valid member of this guild, ${getInsult()}.`
// If the user cannot kick the person..
if (
checkRolePosition(message.member.guild.members.find(i => i.user === user)) >=
checkRolePosition(message.member)
) {
return `You cannot kick this person, you ${getInsult()}.`
}
// Now we kick the person.
try {
await client.kickGuildMember(message.member.guild.id, user.id, args.join(' '))
} catch (e) { return 'I am unable to kick that user.' }
client.createMessage((await client.getDMChannel(user.id)).id, args.length !== 0
? `You have been kicked from ${message.member.guild.name} for ${args.join(' ')}.`
: `You have been kicked from ${message.member.guild.name}.`
)
// WeChill
if (message.member.guild.id === '402423671551164416') {
client.createMessage('402437089557217290', args.length !== 0
? `**${user.username}#${user.discriminator}** has been kicked for **${args.join(' ')}**.`
: `**${user.username}#${user.discriminator}** has been kicked for not staying chill >:L `
)
// WeChill
if (message.member.guild.id === '402423671551164416') {
client.createMessage('402437089557217290', args.length !== 0
? `**${user.username}#${user.discriminator}** has been kicked for **${args.join(' ')}**.`
: `**${user.username}#${user.discriminator}** has been kicked for not staying chill >:L `
)
}
return `**${user.username}#${user.discriminator}** has been kicked. **rip.**`
}
})
return `**${user.username}#${user.discriminator}** has been kicked. **rip.**`
}
}
102 changes: 49 additions & 53 deletions server/bot/commands/admin/ban.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,42 @@ export const handleBan: Command = {
requirements: { permissions: { 'banMembers': true } }
},
aliases: ['banana', 'nuke'],
generators: (client) => ({
generator: async (message, args) => {
// Find the user ID.
const userSpecified = args.shift()
let user: FalseUser|User = getUser(message, userSpecified)
if (!user && client.users.find(i => i.username === userSpecified)) {
user = client.users.find(i => i.username === userSpecified)
} else if (!user && client.users.find(i => i.id === userSpecified)) {
user = client.users.find(i => i.id === userSpecified)
} else if (!user && userSpecified.length === 18 && !isNaN(+userSpecified)) {
user = { id: userSpecified, username: 'Unknown', discriminator: 'user' }
} else return 'I cannot find that user.'
if (!user) return `Specify a valid member of this guild, ${getInsult()}.`
// If the user cannot ban the person..
if (
checkRolePosition(message.member.guild.members.find(i => i.user === user)) >=
checkRolePosition(message.member)
) {
return `You cannot ban this person, you ${getInsult()}.`
}
// Now we ban the person.
try {
await client.banGuildMember(message.member.guild.id, user.id, 0, args.join(' '))
} catch (e) { return 'That person could not be banned.' }
client.createMessage((await client.getDMChannel(user.id)).id, args.length !== 0
? `You have been banned from ${message.member.guild.name} for ${args.join(' ')}.`
: `You have been banned from ${message.member.guild.name}.`
generator: (client) => async (message, args) => {
// Find the user ID.
const userSpecified = args.shift()
let user: FalseUser|User = getUser(message, userSpecified)
if (!user && client.users.find(i => i.username === userSpecified)) {
user = client.users.find(i => i.username === userSpecified)
} else if (!user && client.users.find(i => i.id === userSpecified)) {
user = client.users.find(i => i.id === userSpecified)
} else if (!user && userSpecified.length === 18 && !isNaN(+userSpecified)) {
user = { id: userSpecified, username: 'Unknown', discriminator: 'user' }
} else return 'I cannot find that user.'
if (!user) return `Specify a valid member of this guild, ${getInsult()}.`
// If the user cannot ban the person..
if (
checkRolePosition(message.member.guild.members.find(i => i.user === user)) >=
checkRolePosition(message.member)
) {
return `You cannot ban this person, you ${getInsult()}.`
}
// Now we ban the person.
try {
await client.banGuildMember(message.member.guild.id, user.id, 0, args.join(' '))
} catch (e) { return 'That person could not be banned.' }
client.createMessage((await client.getDMChannel(user.id)).id, args.length !== 0
? `You have been banned from ${message.member.guild.name} for ${args.join(' ')}.`
: `You have been banned from ${message.member.guild.name}.`
)
// WeChill
if (message.member.guild.id === '402423671551164416') {
client.createMessage('402437089557217290', args.length !== 0
? `**${user.username}#${user.discriminator}** has been banned for **${args.join(' ')}**.`
: `**${user.username}#${user.discriminator}** has been banned for not staying chill >:L `
)
// WeChill
if (message.member.guild.id === '402423671551164416') {
client.createMessage('402437089557217290', args.length !== 0
? `**${user.username}#${user.discriminator}** has been banned for **${args.join(' ')}**.`
: `**${user.username}#${user.discriminator}** has been banned for not staying chill >:L `
)
}
return `**${user.username}#${user.discriminator}** has been banned. **rip.**`
}
})
return `**${user.username}#${user.discriminator}** has been banned. **rip.**`
}
}

export const handleUnban: Command = {
Expand All @@ -64,21 +62,19 @@ export const handleUnban: Command = {
guildOnly: true,
requirements: { permissions: { 'banMembers': true } }
},
generators: (client) => ({
generator: async (message, args) => {
// Find the user ID.
const userSpecified = args.shift()
let user: User
if (client.users.find(i => i.username === userSpecified)) {
user = client.users.find(i => i.username === userSpecified)
} else if (client.users.find(i => i.id === userSpecified)) {
user = client.users.find(i => i.id === userSpecified)
} else return 'I cannot find that user.'
// Now we unban the person.
try {
await client.unbanGuildMember(message.member.guild.id, user.id, args.join(' '))
} catch (e) { return 'That user could not be unbanned.' }
return `**${user.username}#${user.discriminator}** has been unbanned.`
}
})
generator: (client) => async (message, args) => {
// Find the user ID.
const userSpecified = args.shift()
let user: User
if (client.users.find(i => i.username === userSpecified)) {
user = client.users.find(i => i.username === userSpecified)
} else if (client.users.find(i => i.id === userSpecified)) {
user = client.users.find(i => i.id === userSpecified)
} else return 'I cannot find that user.'
// Now we unban the person.
try {
await client.unbanGuildMember(message.member.guild.id, user.id, args.join(' '))
} catch (e) { return 'That user could not be unbanned.' }
return `**${user.username}#${user.discriminator}** has been unbanned.`
}
}
Loading

0 comments on commit 34beee2

Please sign in to comment.