update enums to discord-interactions

This commit is contained in:
Shay
2022-04-08 09:54:58 -07:00
parent d2afdabfaa
commit e7aca7a22b
7 changed files with 48 additions and 49 deletions

36
app.js
View File

@@ -1,13 +1,13 @@
import 'dotenv/config'; import 'dotenv/config';
import express from 'express'; import express from 'express';
import { InteractionType, InteractionResponseType, InteractionResponseFlags } from 'discord-interactions';
import { import {
VerifyDiscordRequest, InteractionType,
getRandomEmoji, InteractionResponseType,
ComponentType, InteractionResponseFlags,
ButtonStyle, MessageComponentTypes,
DiscordRequest, ButtonStyleTypes,
} from './utils.js'; } from 'discord-interactions';
import { VerifyDiscordRequest, getRandomEmoji, DiscordRequest } from './utils.js';
import { getShuffledOptions, getResult } from './game.js'; import { getShuffledOptions, getResult } from './game.js';
import { import {
CHALLENGE_COMMAND, CHALLENGE_COMMAND,
@@ -64,7 +64,7 @@ app.post('/interactions', async function (req, res) {
// Create active game using message ID as the game ID // Create active game using message ID as the game ID
activeGames[id] = { activeGames[id] = {
id: userId, id: userId,
objectName: objectName, objectName,
}; };
return res.send({ return res.send({
@@ -74,14 +74,14 @@ app.post('/interactions', async function (req, res) {
content: `Rock papers scissors challenge from <@${userId}>`, content: `Rock papers scissors challenge from <@${userId}>`,
components: [ components: [
{ {
type: ComponentType.ACTION, type: MessageComponentTypes.ACTION_ROW,
components: [ components: [
{ {
type: ComponentType.BUTTON, type: MessageComponentTypes.BUTTON,
// Append the game ID to use later on // Append the game ID to use later on
custom_id: `accept_button_${req.body.id}`, custom_id: `accept_button_${req.body.id}`,
label: 'Accept', label: 'Accept',
style: ButtonStyle.PRIMARY, style: ButtonStyleTypes.PRIMARY,
}, },
], ],
}, },
@@ -114,10 +114,10 @@ app.post('/interactions', async function (req, res) {
flags: InteractionResponseFlags.EPHEMERAL, flags: InteractionResponseFlags.EPHEMERAL,
components: [ components: [
{ {
type: ComponentType.ACTION, type: MessageComponentTypes.ACTION_ROW,
components: [ components: [
{ {
type: ComponentType.SELECT, type: MessageComponentTypes.STRING_SELECT,
// Append game ID // Append game ID
custom_id: `select_choice_${gameId}`, custom_id: `select_choice_${gameId}`,
options: getShuffledOptions(), options: getShuffledOptions(),
@@ -127,7 +127,7 @@ app.post('/interactions', async function (req, res) {
], ],
}, },
}); });
// Delete previous message
await DiscordRequest(endpoint, { method: 'DELETE' }); await DiscordRequest(endpoint, { method: 'DELETE' });
} catch (err) { } catch (err) {
console.error('Error sending message:', err); console.error('Error sending message:', err);
@@ -157,13 +157,13 @@ app.post('/interactions', async function (req, res) {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE, type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: { content: resultStr }, data: { content: resultStr },
}); });
// Update ephemeral message
await DiscordRequest(endpoint, { await DiscordRequest(endpoint, {
method: 'PATCH', method: 'PATCH',
body: { body: {
content: "Nice choice " + getRandomEmoji(), content: 'Nice choice ' + getRandomEmoji(),
components: [] components: [],
} },
}); });
} catch (err) { } catch (err) {
console.error('Error sending message:', err); console.error('Error sending message:', err);

View File

@@ -1,7 +1,12 @@
import 'dotenv/config'; import 'dotenv/config';
import express from 'express'; import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions'; import {
import { VerifyDiscordRequest, ComponentType, ButtonStyle } from '../utils.js'; InteractionType,
InteractionResponseType,
MessageComponentTypes,
ButtonStyleTypes,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app // Create and configure express app
const app = express(); const app = express();
@@ -24,14 +29,14 @@ app.post('/interactions', function (req, res) {
// Buttons are inside of action rows // Buttons are inside of action rows
components: [ components: [
{ {
type: ComponentType.ACTION, type: MessageComponentTypes.ACTION_ROW,
components: [ components: [
{ {
type: ComponentType.BUTTON, type: MessageComponentTypes.BUTTON,
// Value for your app to identify the button // Value for your app to identify the button
custom_id: 'my_button', custom_id: 'my_button',
label: 'Click', label: 'Click',
style: ButtonStyle.PRIMARY, style: ButtonStyleTypes.PRIMARY,
}, },
], ],
}, },

View File

@@ -1,3 +1,4 @@
import 'dotenv/config';
import express from 'express'; import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions'; import { InteractionType, InteractionResponseType } from 'discord-interactions';
import { VerifyDiscordRequest, DiscordRequest } from '../utils.js'; import { VerifyDiscordRequest, DiscordRequest } from '../utils.js';

View File

@@ -1,7 +1,11 @@
import 'dotenv/config'; import 'dotenv/config';
import express from 'express'; import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions'; import {
import { VerifyDiscordRequest, ComponentType } from '../utils.js'; InteractionType,
InteractionResponseType,
MessageComponentTypes,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app // Create and configure express app
const app = express(); const app = express();
@@ -25,11 +29,11 @@ app.post('/interactions', function (req, res) {
components: [ components: [
{ {
// Text inputs must be inside of an action component // Text inputs must be inside of an action component
type: ComponentType.ACTION, type: MessageComponentTypes.ACTION_ROW,
components: [ components: [
{ {
// See https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure // See https://discord.com/developers/docs/interactions/message-components#text-inputs-text-input-structure
type: ComponentType.INPUT, type: MessageComponentTypes.INPUT_TEXT,
custom_id: 'my_text', custom_id: 'my_text',
style: 1, style: 1,
label: 'Type some text', label: 'Type some text',
@@ -37,10 +41,10 @@ app.post('/interactions', function (req, res) {
], ],
}, },
{ {
type: ComponentType.ACTION, type: MessageComponentTypes.ACTION_ROW,
components: [ components: [
{ {
type: ComponentType.INPUT, type: MessageComponentTypes.INPUT_TEXT,
custom_id: 'my_longer_text', custom_id: 'my_longer_text',
// Bigger text box for input // Bigger text box for input
style: 2, style: 2,

View File

@@ -1,7 +1,11 @@
import 'dotenv/config'; import 'dotenv/config';
import express from 'express'; import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions'; import {
import { VerifyDiscordRequest, ComponentType } from '../utils.js'; InteractionType,
InteractionResponseType,
MessageComponentTypes,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app // Create and configure express app
const app = express(); const app = express();
@@ -24,10 +28,10 @@ app.post('/interactions', function (req, res) {
// Selects are inside of action rows // Selects are inside of action rows
components: [ components: [
{ {
type: ComponentType.ACTION, type: MessageComponentTypes.ACTION_ROW,
components: [ components: [
{ {
type: ComponentType.SELECT, type: MessageComponentTypes.STRING_SELECT,
// Value for your app to identify the select menu interactions // Value for your app to identify the select menu interactions
custom_id: 'my_select', custom_id: 'my_select',
// Select options - see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure // Select options - see https://discord.com/developers/docs/interactions/message-components#select-menu-object-select-option-structure

View File

@@ -10,7 +10,7 @@
"author": "Shay DeWael", "author": "Shay DeWael",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"discord-interactions": "^3.1.0", "discord-interactions": "^3.2.0",
"dotenv": "^16.0.0", "dotenv": "^16.0.0",
"express": "^4.17.3", "express": "^4.17.3",
"node-fetch": "^3.2.3" "node-fetch": "^3.2.3"

View File

@@ -39,18 +39,3 @@ export function getRandomEmoji() {
export function capitalize(str) { export function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1); return str.charAt(0).toUpperCase() + str.slice(1);
} }
export const ComponentType = {
ACTION: 1,
BUTTON: 2,
SELECT: 3,
INPUT: 4,
};
export const ButtonStyle = {
PRIMARY: 1,
SECONDARY: 2,
SUCCESS: 3,
DANGER: 4,
LINK: 5,
};