mirror of
https://github.com/cassoule/flopobot_v2.git
synced 2026-03-18 21:40:27 +01:00
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
import 'dotenv/config';
|
|
import express from 'express';
|
|
import {
|
|
InteractionType,
|
|
InteractionResponseType,
|
|
InteractionResponseFlags,
|
|
MessageComponentTypes,
|
|
ButtonStyleTypes,
|
|
} from 'discord-interactions';
|
|
import { VerifyDiscordRequest, getRandomEmoji, DiscordRequest } from './utils.js';
|
|
import { getShuffledOptions, getResult } from './game.js';
|
|
|
|
// Create an express app
|
|
const app = express();
|
|
// Get port, or default to 3000
|
|
const PORT = process.env.PORT || 3000;
|
|
// Parse request body and verifies incoming requests using discord-interactions package
|
|
app.use(express.json({ verify: VerifyDiscordRequest(process.env.PUBLIC_KEY) }));
|
|
|
|
// Store for in-progress games. In production, you'd want to use a DB
|
|
const activeGames = {};
|
|
|
|
/**
|
|
* Interactions endpoint URL where Discord will send HTTP requests
|
|
*/
|
|
app.post('/interactions', async function (req, res) {
|
|
// Interaction type and data
|
|
const { type, id, 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 } = data;
|
|
|
|
// "test" command
|
|
if (name === 'test') {
|
|
// Send a message into the channel where command was triggered from
|
|
return res.send({
|
|
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
|
|
data: {
|
|
// Fetches a random emoji to send from a helper function
|
|
content: 'hello world ' + getRandomEmoji(),
|
|
},
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log('Listening on port', PORT);
|
|
});
|