Command framework mostly set up

This commit is contained in:
Kalle 2022-06-15 23:32:11 +03:00
parent f1991cff61
commit 44072bce7f
7 changed files with 189 additions and 10 deletions

View File

@ -0,0 +1,5 @@
import { questionCommand } from "./q";
export const commands = [questionCommand];
export const commandsMap = Object.fromEntries(commands.map((c) => [c.name, c]));

130
discord-bot/commands/q.ts Normal file
View File

@ -0,0 +1,130 @@
import type { BotCommand } from "../types";
import ids from "../ids";
import { SlashCommandBuilder } from "@discordjs/builders";
const groupedLinks = {
sg: {
seeking: [
"https://cdn.discordapp.com/attachments/865925063731445760/865925984065552404/SG_NT_Seeking.png",
"https://cdn.discordapp.com/attachments/865925063731445760/865926005776056360/SG_HT_Seeking.png",
],
grillers: [
"https://cdn.discordapp.com/attachments/951441475034558484/951441708745388062/SG_NT_Grillers.png",
"https://cdn.discordapp.com/attachments/951441475034558484/951441742283018291/SG_HT_Grillers.png",
],
rush: [
"https://cdn.discordapp.com/attachments/951442362775765022/951442493927485450/SG_NT_Rush.png",
"https://cdn.discordapp.com/attachments/951442362775765022/951442573971578880/SG_HT_Rush.png",
],
},
mb: {
seeking: [
"https://cdn.discordapp.com/attachments/865925063731445760/865926250963140628/MB_NT_Seeking.png",
"https://cdn.discordapp.com/attachments/865925063731445760/865926264409030696/MB_HT_Seeking.png",
],
grillers: [
"https://cdn.discordapp.com/attachments/951441475034558484/951441787573141534/MB_NT_Grillers.png",
"https://cdn.discordapp.com/attachments/951441475034558484/951441828299817020/MB_HT_Grillers.png",
],
rush: [
"https://cdn.discordapp.com/attachments/951442362775765022/951442753563291688/MB_NT_Rush.png",
"https://cdn.discordapp.com/attachments/951442362775765022/951442772345356288/MB_HT_Rush.png",
],
},
lo: {
seeking: [
"https://cdn.discordapp.com/attachments/865925063731445760/865926336199655424/LO_NT_Seeking.png",
"https://cdn.discordapp.com/attachments/865925063731445760/865926346545954836/LO_HT_Seeking.png",
],
grillers: [
"https://cdn.discordapp.com/attachments/951441475034558484/951441880841854986/LO_NT_Grillers.png",
"https://cdn.discordapp.com/attachments/951441475034558484/951441897052864532/LO_HT_Grillers.png",
],
rush: [
"https://cdn.discordapp.com/attachments/951442362775765022/951442800166174782/LO_NT_Rush.png",
"https://cdn.discordapp.com/attachments/951442362775765022/951442813596360714/LO_HT_Rush.png",
],
},
ss: {
seeking: [
"https://cdn.discordapp.com/attachments/865925063731445760/865926389223784468/SS_NT_Seeking.png",
"https://cdn.discordapp.com/attachments/865925063731445760/865926397561798656/SS_HT_Seeking.png",
],
grillers: [
"https://cdn.discordapp.com/attachments/951441475034558484/951441959510233118/SS_NT_Grillers.png",
"https://cdn.discordapp.com/attachments/951441475034558484/951441974198698005/SS_HT_Grillers.png",
],
rush: [
"https://cdn.discordapp.com/attachments/951442362775765022/951442852485935164/SS_NT_Rush.png",
"https://cdn.discordapp.com/attachments/951442362775765022/951442864305479710/SS_HT_Rush.png",
],
},
ap: {
seeking: [
"https://cdn.discordapp.com/attachments/865925063731445760/865926537629663242/AP_NT_Seeking.png",
"https://cdn.discordapp.com/attachments/865925063731445760/865926546635620382/AP_HT_Seeking.png",
],
grillers: [
"https://cdn.discordapp.com/attachments/951441475034558484/951442012626878514/AP_NT_Grillers.png",
"https://cdn.discordapp.com/attachments/951441475034558484/951442021246201906/AP_HT_Grillers.png",
],
rush: [
"https://cdn.discordapp.com/attachments/951442362775765022/951442890721206312/AP_NT_Rush.png",
"https://cdn.discordapp.com/attachments/951442362775765022/951442901219561472/AP_HT_Rush.png",
],
},
fundamentals:
"https://docs.google.com/document/d/1WTDiKSzV3SaD8Sn7wWlGreE2Y4Y7UrOv3SSv7qtzvcg/edit?usp=sharing",
fundamentals2: "https://youtu.be/BvGbmLO94Po",
advanced:
"https://docs.google.com/document/d/16wcdlxqwqBvOnXIviB_ig0FS5_a96DKwYUIiGh6YHa8/edit?usp=sharing",
spawns:
"https://cdn.discordapp.com/attachments/836884751922757652/836886520250105886/SR_Time_Interval.png",
ballpoint: "https://youtu.be/oSf39Yn0wOU",
explo: "https://youtu.be/1qSW0_fA_L4",
dynamo: "https://youtu.be/GrCCAI8giKc",
bamboo: "https://youtu.be/yptwgqIoYiI",
hydra: "https://youtu.be/_YduyOTVoLk",
} as const;
const COMMAND_NAME = "q";
export const questionCommand: BotCommand = {
name: COMMAND_NAME,
builder: new SlashCommandBuilder()
.setName(COMMAND_NAME)
.setDescription("wohoo"),
execute: async ({ interaction }) => {
return interaction.reply("Pong!");
},
};
// const questionCommand: BotCommand = {
// name: "add",
// description: "Gives you the specified role",
// validGuilds: [ids.guilds.sro],
// execute: async ({ interaction }) => {
// let result: any = groupedLinks;
// console.log("args", args);
// for (const arg of args) {
// console.log("result", result);
// if (typeof result === "string" || !result) break;
// if (result.hasOwnProperty(arg)) {
// const value = result[arg];
// result = value;
// }
// }
// if (Array.isArray(result)) {
// result = result.join("\n");
// }
// if (typeof result !== "string") {
// await msg.channel.send(
// "Invalid parameters - use: `!q` to see the available parameters"
// );
// return;
// }
// await msg.channel.send(result);
// },
// };

View File

@ -1,19 +1,15 @@
import "dotenv/config";
import { SlashCommandBuilder } from "@discordjs/builders";
import { REST } from "@discordjs/rest";
import { Routes } from "discord-api-types/v9";
import invariant from "tiny-invariant";
import { commands } from "./commands";
invariant(process.env["BOT_TOKEN"], "DISCORD_TOKEN must be set");
invariant(process.env["BOT_ID"], "BOT_ID must be set");
invariant(process.env["TEST_GUILD_ID"], "TEST_GUILD_ID must be set");
const commands = [
new SlashCommandBuilder()
.setName("ping")
.setDescription("Replies with pong!"),
].map((command) => command.toJSON());
const serializedCommands = commands.map((cmd) => cmd.builder.toJSON());
const rest = new REST({ version: "9" }).setToken(process.env["BOT_TOKEN"]);
@ -23,7 +19,7 @@ rest
process.env["BOT_ID"],
process.env["TEST_GUILD_ID"]
),
{ body: commands }
{ body: serializedCommands } // TODO: divide by guild
)
.then(() => console.log("Successfully registered application commands."))
.catch(console.error);

29
discord-bot/ids.ts Normal file
View File

@ -0,0 +1,29 @@
const ids = {
users: {
admin: "79237403620945920",
},
roles: {
sroLfg: "749589703371653158",
sroCoach: "807258345223356486",
plusTwoPings: "678602424314888224",
plusThreePings: "818060819811663872",
plusOne: "657613334249734165",
plusTwo: "678169134416265260",
plusThree: "815267885601521696",
patreonSupporter: "787281384909045760",
patreonSupporterPlus: "787281624152801350",
patreonSupporterPlusPlus: "787281663138725928",
},
channels: {
// Salmon Run // Plus Server
bot: ["746130457455886337", "808995745768013854"],
sroLfg: "745036737943699546",
},
guilds: {
sro: "737359708276654121",
plusServer: "351828612208525333",
sendou: "299182152161951744",
},
} as const;
export default ids;

View File

@ -2,6 +2,7 @@
import { Client, Intents } from "discord.js";
import "dotenv/config";
import invariant from "tiny-invariant";
import { commandsMap } from "./commands";
invariant(process.env["BOT_TOKEN"], "DISCORD_TOKEN must be set");
@ -15,10 +16,15 @@ client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
const command = commandsMap[commandName];
if (commandName === "ping") {
await interaction.reply("Pong!");
if (!command) {
throw new Error(
`I don't know how to handle the command called "${commandName}"`
);
}
return command.execute({ interaction });
});
client.login(process.env["BOT_TOKEN"]).catch((err) => console.error(err));

View File

@ -2,7 +2,7 @@
"name": "lohi",
"scripts": {
"dev": "ts-node index.ts",
"commands": "ts-node commands.ts",
"commands": "ts-node deploy-commands.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {

13
discord-bot/types.ts Normal file
View File

@ -0,0 +1,13 @@
import type { SlashCommandBuilder } from "@discordjs/builders";
import type { CommandInteraction } from "discord.js";
export interface BotCommand {
name: string;
// description: string;
// /** ID's of guilds where this command will be set */
// guilds: string[];
builder: SlashCommandBuilder;
execute: (commandParameters: {
interaction: CommandInteraction;
}) => Promise<void>;
}