From db8b635907ec86bbbc627aef5543e5397387d7d3 Mon Sep 17 00:00:00 2001 From: Ibrahim Ansari Date: Mon, 23 Aug 2021 18:11:43 +0530 Subject: [PATCH] Add commonGenerators, ephemeral errors. Also, port /serverinfo to slash commands. --- src/commands/games.ts | 68 +++++++++++++-------------------------- src/commands/utilities.ts | 35 ++++++++++++++------ src/imports/types.ts | 1 + src/index.ts | 9 ++++-- 4 files changed, 56 insertions(+), 57 deletions(-) diff --git a/src/commands/games.ts b/src/commands/games.ts index 83c467a..813b746 100644 --- a/src/commands/games.ts +++ b/src/commands/games.ts @@ -114,18 +114,10 @@ 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 @@ -133,7 +125,7 @@ export const handleZalgo: Command = { newMessage += characters[Math.floor(Math.random() * characters.length)] } }) - return newMessage.length >= 2000 ? args.join(' ') : newMessage + return newMessage.length >= 2000 ? text : newMessage } } @@ -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 @@ -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 ' - 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 } } @@ -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 } } diff --git a/src/commands/utilities.ts b/src/commands/utilities.ts index 555581b..709a913 100644 --- a/src/commands/utilities.ts +++ b/src/commands/utilities.ts @@ -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' @@ -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) @@ -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 ', - 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. diff --git a/src/imports/types.ts b/src/imports/types.ts index 348de59..b1a4543 100644 --- a/src/imports/types.ts +++ b/src/imports/types.ts @@ -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 } export interface CommandOptions { argsRequired?: boolean diff --git a/src/index.ts b/src/index.ts index 7d0ed76..28a2475 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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. @@ -76,14 +76,19 @@ const commandToSlashCommand = (command: Command): SlashCommand => { } async run (ctx: CommandContext): Promise { + 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)