From 5b27abbba96de1cdcc383d2b7c42e1fb27a4ffe3 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Fri, 13 May 2022 18:57:45 -0400 Subject: [PATCH] Added @mention command to learn about the bot --- db/populateDefaults.ts | 2 +- mod.ts | 12 ++++++++++-- src/commands/_index.ts | 4 +++- src/commands/handleMentions.ts | 22 ++++++++++++++++++++++ src/constantCmds.ts | 9 +++++++++ 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/commands/handleMentions.ts diff --git a/db/populateDefaults.ts b/db/populateDefaults.ts index 84956b4..74229c7 100644 --- a/db/populateDefaults.ts +++ b/db/populateDefaults.ts @@ -10,7 +10,7 @@ await dbClient.execute("INSERT INTO all_keys(userid,apiKey) values(?,?)", [confi console.log("Inesrtion done"); console.log("Attempting to insert default commands into command_cnt"); -const commands = ["ping", "rip", "rollhelp", "help", "info", "version", "report", "stats", "roll", "emojis", "api", "privacy"]; +const commands = ["ping", "rip", "rollhelp", "help", "info", "version", "report", "stats", "roll", "emojis", "api", "privacy", "mention"]; for (let i = 0; i < commands.length; i++) { await dbClient.execute("INSERT INTO command_cnt(command) values(?)", [commands[i]]).catch(e => { console.log(`Failed to insert into database`, e); diff --git a/mod.ts b/mod.ts index 829acda..1ac54b2 100644 --- a/mod.ts +++ b/mod.ts @@ -100,9 +100,17 @@ startBot({ if (message.isBot) return; // Ignore all messages that are not commands - if (message.content.indexOf(config.prefix) !== 0) return; + if (message.content.indexOf(config.prefix) !== 0) { + // Handle @bot messages + if (message.mentionedUserIds[0] === botId && (message.content.trim().startsWith(`<@${botId}>`) || message.content.trim().startsWith(`<@!${botId}>`))) { + commands.handleMentions(message); + } + + // return as we are done handling this command + return; + } - log(LT.LOG, `Handling message ${JSON.stringify(message)}`); + log(LT.LOG, `Handling [[command message: ${JSON.stringify(message)}`); // Split into standard command + args format const args = message.content.slice(config.prefix.length).trim().split(/[ \n]+/g); diff --git a/src/commands/_index.ts b/src/commands/_index.ts index 72b1dcc..cffe162 100644 --- a/src/commands/_index.ts +++ b/src/commands/_index.ts @@ -10,6 +10,7 @@ import { stats } from "./stats.ts"; import { api } from "./apiCmd.ts"; import { emoji } from "./emoji.ts"; import { roll } from "./roll.ts"; +import { handleMentions } from "./handleMentions.ts"; export default { ping, @@ -23,5 +24,6 @@ export default { stats, api, emoji, - roll + roll, + handleMentions }; diff --git a/src/commands/handleMentions.ts b/src/commands/handleMentions.ts new file mode 100644 index 0000000..f2f7e2f --- /dev/null +++ b/src/commands/handleMentions.ts @@ -0,0 +1,22 @@ +import { dbClient } from "../db.ts"; +import { + // Discordeno deps + DiscordenoMessage, + + // Log4Deno deps + LT, log +} from "../../deps.ts"; +import { constantCmds } from "../constantCmds.ts"; + +export const handleMentions = (message: DiscordenoMessage) => { + log(LT.LOG, `Handling @mention message: ${JSON.stringify(message)}`); + + // Light telemetry to see how many times a command is being run + dbClient.execute(`CALL INC_CNT("mention");`).catch(e => { + log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`); + }); + + message.send(constantCmds.mention).catch(e => { + log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + }); +}; diff --git a/src/constantCmds.ts b/src/constantCmds.ts index 57c58df..e10e33e 100644 --- a/src/constantCmds.ts +++ b/src/constantCmds.ts @@ -159,6 +159,15 @@ export const constantCmds = { title: "Compiling latest statistics . . ." }] }, + mention: { + embeds: [{ + title: `Hello! I am ${config.name}!`, + fields: [{ + name: "I am a bot that specializes in rolling dice and doing basic algebra", + value: `To learn about my available commands, please run \`${config.prefix}help\`` + }] + }] + }, privacy: { embeds: [{ title: "Privacy Policy",