Skip to content

Commit

Permalink
Add commonGenerators, ephemeral errors.
Browse files Browse the repository at this point in the history
Also, port /serverinfo to slash commands.
  • Loading branch information
retrixe committed Aug 23, 2021
1 parent c65e505 commit db8b635
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 57 deletions.
68 changes: 22 additions & 46 deletions src/commands/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,26 +114,18 @@ export const handleZalgo: Command = {
type: CommandOptionType.STRING
}]
},
slashGenerator: context => {
let newMessage = ''
context.options.text.split('').forEach((element: string) => {
newMessage += element
for (let i = 0; i < Math.floor(Math.random() * 5) + 1; i++) {
newMessage += characters[Math.floor(Math.random() * characters.length)]
}
})
return newMessage.length >= 2000 ? context.options.text : newMessage
},
generator: (message, args) => {
const textToZalgo = args.join(' ').split('')
generator: (message, args) => handleZalgo.commonGenerator(args.join(' ')),
slashGenerator: context => handleZalgo.commonGenerator(context.options.text),
commonGenerator: (text: string) => {
const textToZalgo = text.split('')
let newMessage = ''
textToZalgo.forEach(element => {
newMessage += element
for (let i = 0; i < Math.floor(Math.random() * 5) + 1; i++) {
newMessage += characters[Math.floor(Math.random() * characters.length)]
}
})
return newMessage.length >= 2000 ? args.join(' ') : newMessage
return newMessage.length >= 2000 ? text : newMessage
}
}

Expand All @@ -152,16 +144,11 @@ export const handleDezalgo: Command = {
type: CommandOptionType.STRING
}]
},
slashGenerator: context => {
slashGenerator: context => handleDezalgo.commonGenerator(context.options.text),
generator: (message, args) => handleDezalgo.commonGenerator(args.join(' ')),
commonGenerator: (text: string) => {
let newMessage = ''
context.options.text.split('').forEach((element: string) => {
if (!characters.includes(element)) newMessage += element
})
return newMessage
},
generator: (message, args) => {
let newMessage = ''
args.join(' ').split('').forEach(element => {
text.split('').forEach(element => {
if (!characters.includes(element)) newMessage += element
})
return newMessage
Expand Down Expand Up @@ -191,27 +178,22 @@ export const handleRepeat: Command = {
slashGenerator: context => {
const number = context.options.number
const text = context.options.text as string
if (number * text.length >= 2001) {
return { content: 'To prevent spam, your excessive message has not been repeated.', error: true }
} else if (text === '_' || text === '*' || text === '~') {
return { content: 'This is known to lag users and is disabled.', error: true }
}
let generatedMessage = ''
for (let x = 0; x < number; x++) { generatedMessage += text }
return generatedMessage
return handleRepeat.commonGenerator(number, text)
},
generator: (message, args) => {
// All arguments.
const number = +args.shift()
if (isNaN(number)) return 'Correct usage: /repeat <number of times> <string to repeat>'
else if (number * args.join(' ').length >= 2001) {
return handleRepeat.commonGenerator(number, args.join(' '))
},
commonGenerator: (number: number, text: string) => {
if (number * text.length >= 2001) {
return { content: 'To prevent spam, your excessive message has not been repeated.', error: true }
} else if (
args.join(' ') === '_' || args.join(' ') === '*' || args.join(' ') === '~'
) return { content: 'This is known to lag users and is disabled.', error: true }
// Generate the repeated string.
} else if (text === '_' || text === '*' || text === '~') {
return { content: 'This is known to lag users and is disabled.', error: true }
}
let generatedMessage = ''
for (let x = 0; x < number; x++) { generatedMessage += args.join(' ') }
for (let x = 0; x < number; x++) { generatedMessage += text }
return generatedMessage
}
}
Expand Down Expand Up @@ -282,17 +264,11 @@ More info here: https://mathjs.org/docs/expressions/syntax.html`,
type: CommandOptionType.STRING
}]
},
slashGenerator: context => {
try {
const expr = context.options.expression
return `${evaluate(expr.split(',').join('.').split('÷').join('/').toLowerCase())}`
} catch (e) {
return { content: 'Invalid expression >_<', error: true }
}
},
generator: (message, args) => {
slashGenerator: context => handleCalculate.commonGenerator(context.options.expression),
generator: (message, args) => handleCalculate.commonGenerator(args.join(' ')),
commonGenerator: (expression: string) => {
try {
return `${evaluate(args.join(' ').split(',').join('.').split('÷').join('/').toLowerCase())}`
return `${evaluate(expression.split(',').join('.').split('÷').join('/').toLowerCase())}`
} catch (e) {
return { content: 'Invalid expression >_<', error: true }
}
Expand Down
35 changes: 26 additions & 9 deletions src/commands/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// All the types!
import Eris, { Message, GuildTextableChannel, Constants } from 'eris'
import Eris, { Message, GuildTextableChannel, Constants, Guild } from 'eris'
import { CommandOptionType } from 'slash-create'
import { Command } from '../imports/types.js'
// All the needs!
import { getIdFromMention, getInsult, getUser } from '../imports/tools.js'
Expand All @@ -15,16 +16,26 @@ export const handleServerinfo: Command = {
fullDescription: 'Displays info on the current servers (or other mutual servers).',
example: '/serverinfo',
usage: '/serverinfo (mutual server ID)',
argsRequired: false
argsRequired: false,
slashOptions: [{
name: 'server',
required: false,
description: 'A server you share with IveBot.',
type: CommandOptionType.STRING
}]
},

slashGenerator: (context, { client }) => {
let guild = client.guilds.get(context.options.server || context.guildID)
if (context.options.server && guild && !guild.members.has(context.member.id)) guild = undefined
return handleServerinfo.commonGenerator(guild)
},
generator: (message, args, { client }) => {
// Check if a guild was specified.
const guild = (args.length > 0)
? client.guilds.find(
i => i.members.has(message.author.id) && i.id === args[0]
)
: message.member.guild
let guild = args.length > 0 ? client.guilds.get(args[0]) : message.member.guild
if (args.length > 0 && guild && !guild.members.has(message.author.id)) guild = undefined
return handleServerinfo.commonGenerator(guild)
},
commonGenerator: (guild: Guild) => {
if (!guild) return { content: `Specify a valid mutual guild, ${getInsult()}.`, error: true }
// Owner.
const owner = guild.members.get(guild.ownerID)
Expand Down Expand Up @@ -239,7 +250,13 @@ export const handleRequest: Command = {
description: 'Request a specific feature.',
fullDescription: 'Request a feature. 24 hour cooldown except for test pilots.',
usage: '/request <suggestion>',
example: '/request a /userinfo command.'
example: '/request a /userinfo command.',
slashOptions: [{
name: 'suggestion',
required: true,
type: CommandOptionType.STRING,
description: 'The feature you want to suggest, or the bug you wish to report. Please be detailed.'
}]
},
generator: async ({ author }, args, { client, tempDB }) => {
// Check for cooldown.
Expand Down
1 change: 1 addition & 0 deletions src/imports/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface Command {
generator: IveBotCommandGenerator
postGenerator?: (message: Message, args: string[], sent?: Message, ctx?: Context) => void
slashGenerator?: true | IveBotSlashGeneratorFunction
commonGenerator?: (...args: any[]) => CommandResponse | Promise<CommandResponse>
}
export interface CommandOptions {
argsRequired?: boolean
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'json5/lib/require.js'
// Tokens and stuff.
import { Client, MessageWebhookContent } from 'eris'
import { SlashCommand, SlashCreator, GatewayServer, CommandContext } from 'slash-create'
import { SlashCommand, SlashCreator, GatewayServer, CommandContext, InteractionResponseFlags } from 'slash-create'
// Get MongoDB.
import { MongoClient } from 'mongodb'
// Import fs.
Expand Down Expand Up @@ -76,14 +76,19 @@ const commandToSlashCommand = (command: Command): SlashCommand => {
}

async run (ctx: CommandContext): Promise<string | MessageWebhookContent | void> {
if (command.opts.guildOnly && !ctx.guildID) return 'This command can only be executed from a Discord guild!'
if (typeof command.generator !== 'function') return command.generator
const func: IveBotSlashGeneratorFunction = command.slashGenerator === true
? command.generator as any
: command.slashGenerator
const response = await Promise.resolve(func(ctx, { client, db, tempDB, commandParser }))
return typeof response === 'object' && response.embed
const embedResponse = typeof response === 'object' && response.embed
? { ...response, embeds: [response.embed] }
: response
if (typeof embedResponse === 'object' && embedResponse.error) {
embedResponse.flags = InteractionResponseFlags.EPHEMERAL
}
return embedResponse
}
}
return new IveBotSlashCommand(creator)
Expand Down

0 comments on commit db8b635

Please sign in to comment.