aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjacob janzen <53062115+JacobJanzen@users.noreply.github.com>2024-12-11 21:40:39 -0600
committerGitHub <noreply@github.com>2024-12-11 21:40:39 -0600
commitc25197dbe7a054022739190e41c12d09e79f4c04 (patch)
tree272cb9caf95933c49102a6711827c2a74596f381
parent36066ad3d8d92855bbfdf4273a53066c6092fa45 (diff)
Add Buttons; Remove Unschedule Command (#5)
-rw-r--r--app.js27
-rw-r--r--command_impls.js45
-rw-r--r--commands.js15
-rw-r--r--message-scheduler.js13
4 files changed, 48 insertions, 52 deletions
diff --git a/app.js b/app.js
index f445a8f..5828b31 100644
--- a/app.js
+++ b/app.js
@@ -6,13 +6,7 @@ import {
verifyKeyMiddleware,
} from "discord-interactions";
import { Client, GatewayIntentBits, ActivityType } from "discord.js";
-import {
- blep,
- help,
- pet,
- schedule_message,
- unschedule_message,
-} from "./command_impls.js";
+import { blep, help, pet, schedule_message } from "./command_impls.js";
import { MessageSchedule } from "./message-scheduler.js";
class State {
@@ -34,9 +28,6 @@ function handle_application_command(state, data, channel_id) {
options[1].value,
);
- case "unschedule_message":
- return unschedule_message(state, options[0].value);
-
case "pet":
return pet(state);
@@ -52,6 +43,20 @@ function handle_application_command(state, data, channel_id) {
}
}
+function handle_button_press(state, data) {
+ if (data.custom_id.startsWith("stop-")) {
+ state.schedule.unschedule(data.custom_id.slice(5));
+
+ return state.res.send({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: "no longer sending this message",
+ },
+ });
+ }
+ return res.status(400).json({ error: "unknown command" });
+}
+
function main() {
const state = new State();
@@ -86,6 +91,8 @@ function main() {
return res.send({ type: InteractionResponseType.PONG });
case InteractionType.APPLICATION_COMMAND:
return handle_application_command(state, data, channel_id);
+ case InteractionType.MESSAGE_COMPONENT:
+ return handle_button_press(state, data);
default:
console.error("unknown interaction type", type);
return res
diff --git a/command_impls.js b/command_impls.js
index d10b293..fdd8219 100644
--- a/command_impls.js
+++ b/command_impls.js
@@ -1,12 +1,11 @@
import { InteractionResponseType } from "discord-interactions";
import { Message } from "./message-scheduler.js";
+import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
-function send(state, content) {
+function send(state, data) {
return state.res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
- data: {
- content: content,
- },
+ data: data,
});
}
@@ -15,40 +14,34 @@ export function schedule_message(state, msg, channel, crontab) {
const schedule_valid = state.schedule.schedule(message);
- return send(
- state,
- schedule_valid
- ? `registered message: "${msg}" with cron: "${crontab}" and id: "${message.id}"`
- : "invalid cron",
- );
-}
-
-export function unschedule_message(state, id) {
- const unschedule_valid = state.schedule.unschedule(id);
+ const cancel = new ButtonBuilder()
+ .setCustomId(`stop-${message.id}`)
+ .setLabel("Cancel")
+ .setStyle(ButtonStyle.Secondary);
- return send(
- state,
- unschedule_valid ? `stopped job ${id}` : `no such job ${id}`,
- );
+ return send(state, {
+ content: schedule_valid
+ ? `registered message: "${msg}" with cron: "${crontab}"`
+ : "invalid cron",
+ components: [new ActionRowBuilder().addComponents(cancel)],
+ });
}
export function pet(state) {
- return send(state, "[purring noises]");
+ return send(state, { content: "[purring noises]" });
}
export function help(state) {
- return send(
- state,
- `Hi, I'm sily-bot!
+ return send(state, {
+ content: `Hi, I'm sily-bot!
Here are the available commands and their descriptions:
- \`/blep\` blep.
- \`/help\` Show this message.
- \`/pet\` You can pet sily-bot.
-- \`/schedule-message <message> <cron>\` Schedule a message to be send later. Works like Linux cron jobs in the format second minute hour day month weekday. Put the number (or name of month or weekday) in each spot. If you want it to run every second, minute, etc. instead of once when it reaches the provided number, use a \`*\` instead of a number. For instance, to run a job every minute on January 4th, you might use \`0 * * 4 January *\`. The bot replies with a UUID that can be used to cancel the cron job later.
-- \`/unschedule-message <id>\` Stop sending a message with the given id.`,
- );
+- \`/schedule-message <message> <cron>\` Schedule a message to be send later. Works like Linux cron jobs in the format second minute hour day month weekday. Put the number (or name of month or weekday) in each spot. If you want it to run every second, minute, etc. instead of once when it reaches the provided number, use a \`*\` instead of a number. For instance, to run a job every minute on January 4th, you might use \`0 * * 4 January *\`.`,
+ });
}
export function blep(state) {
- return send(state, `≽^•𐃷•^≼`);
+ return send(state, "≽^•𐃷•^≼");
}
diff --git a/commands.js b/commands.js
index 2c35d5e..97b7265 100644
--- a/commands.js
+++ b/commands.js
@@ -45,21 +45,6 @@ const commands = [
integration_types: [0],
contexts: [0],
},
- {
- name: "unschedule_message",
- description: "Stop sending a scheduled message",
- type: 1,
- options: [
- {
- type: 3,
- name: "id",
- description: "the id of the message to stop",
- required: true,
- },
- ],
- integration_types: [0],
- contexts: [0],
- },
];
const rest = new REST({ version: "10" }).setToken(process.env.DISCORD_TOKEN);
diff --git a/message-scheduler.js b/message-scheduler.js
index 12f868b..99d9b77 100644
--- a/message-scheduler.js
+++ b/message-scheduler.js
@@ -2,6 +2,7 @@ import "dotenv/config";
import cron from "node-cron";
import { v4 as uuidv4 } from "uuid";
import fs from "node:fs";
+import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
export class Message {
constructor(message, channel_id, crontab, repeat = true, id = null) {
@@ -49,12 +50,22 @@ export class MessageSchedule {
if (valid) {
this.jobs[message.id] = message;
+ const stop = new ButtonBuilder()
+ .setCustomId(`stop-${message.id}`)
+ .setLabel("Stop")
+ .setStyle(ButtonStyle.Danger);
+
this.crons[message.id] = cron.schedule(
message.crontab,
() => {
this.state.client.channels.cache
.get(message.channel_id)
- .send(message.message);
+ .send({
+ content: message.message,
+ components: [
+ new ActionRowBuilder().addComponents(stop),
+ ],
+ });
if (!message.repeat) {
this.unschedule(message.id);
}