aboutsummaryrefslogtreecommitdiff
path: root/app.js
diff options
context:
space:
mode:
authorJacob Janzen <jacob@jjanzen.ca>2024-12-08 01:56:35 +0000
committerJacob Janzen <jacob@jjanzen.ca>2024-12-08 01:56:35 +0000
commit0fbf84b271709582fb328941382f563d15e129eb (patch)
treee6b142684e092b54dd036f04a714b3944270c00e /app.js
initial commit
Diffstat (limited to 'app.js')
-rw-r--r--app.js68
1 files changed, 68 insertions, 0 deletions
diff --git a/app.js b/app.js
new file mode 100644
index 0000000..c387daf
--- /dev/null
+++ b/app.js
@@ -0,0 +1,68 @@
+import 'dotenv/config';
+import express from 'express';
+import cron from 'node-cron';
+import {
+ InteractionType,
+ InteractionResponseType,
+ verifyKeyMiddleware,
+} from 'discord-interactions';
+
+// Create an express app
+const app = express();
+// Get port, or default to 3000
+const PORT = process.env.PORT || 3000;
+
+/**
+ * Interactions endpoint URL where Discord will send HTTP requests
+ * Parse request body and verifies incoming requests using discord-interactions package
+ */
+app.post('/', verifyKeyMiddleware(process.env.PUBLIC_KEY), async function (req, res) {
+ // Interaction type and data
+ const { type, data } = req.body;
+
+ /**
+ * Handle verification requests
+ */
+ if (type === InteractionType.PING) {
+ return res.send({ type: InteractionResponseType.PONG });
+ }
+
+ /**
+ * Handle slash command requests
+ * See https://discord.com/developers/docs/interactions/application-commands#slash-commands
+ */
+ if (type === InteractionType.APPLICATION_COMMAND) {
+ const { name, options } = data;
+
+ if (name === 'schedule_message') {
+ const message = options[0].value;
+ const crontab = options[1].value;
+ const valid = cron.validate(crontab);
+
+ const content = valid ? 'registered message "' + message + '" with cron "' + crontab + '"' : 'invalid cron';
+
+ if (valid) {
+ cron.schedule(crontab, () => {
+ console.log(message);
+ });
+ }
+
+ return res.send({
+ type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
+ data: {
+ content: content,
+ },
+ });
+ }
+
+ console.error(`unknown command: ${name}`);
+ return res.status(400).json({ error: 'unknown command' });
+ }
+
+ console.error('unknown interaction type', type);
+ return res.status(400).json({ error: 'unknown interaction type' });
+});
+
+app.listen(PORT, () => {
+ console.log('Listening on port', PORT);
+});