From 2b1ce426380ad38c9c9d1d0be6a1f9996effcb76 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Wed, 20 Mar 2024 23:03:26 -0400 Subject: [PATCH] Add discord slash commands --- config/discord.yml | 2 + src/api/api/trpc.ts | 17 ++++++++ src/discord/bot/Bot.ts | 88 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 106 insertions(+), 1 deletion(-) diff --git a/config/discord.yml b/config/discord.yml index f6b1962..71e2ef8 100644 --- a/config/discord.yml +++ b/config/discord.yml @@ -1,2 +1,4 @@ +# serverID: "1218101891838251060" +# defaultChannelID: "1218101892660330578" serverID: "841331769051578413" defaultChannelID: "841331769658703954" diff --git a/src/api/api/trpc.ts b/src/api/api/trpc.ts index 570eb5e..cba1c76 100644 --- a/src/api/api/trpc.ts +++ b/src/api/api/trpc.ts @@ -1,4 +1,5 @@ import { getBacks, flushBacks } from "@server/backs"; +import { commandGroups } from "@server/commands/groups"; import { handleCommand } from "@server/commands/handler"; import { prefixes } from "@server/commands/prefixes"; import { checkToken, tokenToID } from "@server/data/token"; @@ -52,6 +53,22 @@ export const appRouter = router({ return prefixes; }), + commandList: publicProcedure.query(async opts => { + let groups = []; + + for (const group of commandGroups) { + let commands = group.commands.filter(cmd => cmd.visible); + + groups.push({ + id: group.id, + displayName: group.displayName, + commands + }); + } + + return groups; + }), + command: privateProcedure .input( z.object({ diff --git a/src/discord/bot/Bot.ts b/src/discord/bot/Bot.ts index 614c0a0..b45c7ed 100644 --- a/src/discord/bot/Bot.ts +++ b/src/discord/bot/Bot.ts @@ -1,5 +1,5 @@ import { EventEmitter } from "events"; -import Discord from "discord.js"; +import Discord, { SlashCommandBuilder } from "discord.js"; import { Logger } from "@util/Logger"; import { CosmicColor } from "@util/CosmicColor"; import gettRPC from "@util/api/trpc"; @@ -53,6 +53,37 @@ export class DiscordBot extends EventEmitter { if (!channel) throw "Unable to find default Discord channel."; this.defaultChannel = channel as Discord.TextChannel; + + const groups = await this.trpc.commandList.query(); + if (!groups) throw "Unable to get command list."; + + const builders = []; + + for (const group of groups) { + for (const command of group.commands) { + const builder = new SlashCommandBuilder(); + builder.setName(command.aliases[0]); + builder.setDescription(command.description); + builder.addStringOption(option => + option + .setName("args") + .setDescription("Command arguments") + ); + + builders.push(builder); + } + } + + const rest = new Discord.REST().setToken(this.token || ""); + rest.put( + Discord.Routes.applicationGuildCommands( + this.client.user?.id || "", + this.conf.serverID + ), + { + body: builders + } + ); }); this.client.on("guildMemberAdd", async member => { @@ -193,5 +224,60 @@ export class DiscordBot extends EventEmitter { msg.message.split(`@${msg.id}`).join(`<@${msg.id}>`) ); }); + + this.client.on("interactionCreate", async msg => { + if (!this.server) return; + if (msg.guildId !== this.server.id) return; + + if (!msg.isCommand()) return; + + const existingRole = this.server.roles.cache.find( + role => role.name === msg.user.id + ); + + if (!existingRole) return; + + let prefixes: string[]; + + try { + prefixes = await this.trpc.prefixes.query(); + } catch (err) { + this.logger.error(err); + this.logger.warn("Unable to contact server"); + return; + } + + let usedPrefix = prefixes[0]; + if (!usedPrefix) return; + + this.logger.debug("Args:", msg.options.data); + + const argsOption = msg.options.get("args"); + const args = argsOption + ? argsOption.value + ? argsOption.value.toString().split(" ") + : [] + : []; + + const command = await this.trpc.command.query({ + channel: msg.channelId || this.conf.defaultChannelID, + args: args, + command: msg.commandName, + prefix: usedPrefix, + user: { + id: msg.user.id, + name: msg.user.displayName, + color: existingRole.hexColor + } + }); + + if (!command) return; + if (command.response) + msg.reply( + command.response + .split(`@${msg.user.id}`) + .join(`<@${msg.user.id}>`) + ); + }); } }