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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,11 @@
import 'dotenv/config';
import express from 'express';
import { InteractionType, InteractionResponseType } from 'discord-interactions';
import { VerifyDiscordRequest, ComponentType } from '../utils.js';
import {
InteractionType,
InteractionResponseType,
MessageComponentTypes,
} from 'discord-interactions';
import { VerifyDiscordRequest } from '../utils.js';
// Create and configure express app
const app = express();
@@ -24,10 +28,10 @@ app.post('/interactions', function (req, res) {
// Selects are inside of action rows
components: [
{
type: ComponentType.ACTION,
type: MessageComponentTypes.ACTION_ROW,
components: [
{
type: ComponentType.SELECT,
type: MessageComponentTypes.STRING_SELECT,
// Value for your app to identify the select menu interactions
custom_id: 'my_select',
// 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",
"license": "MIT",
"dependencies": {
"discord-interactions": "^3.1.0",
"discord-interactions": "^3.2.0",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"node-fetch": "^3.2.3"

View File

@@ -39,18 +39,3 @@ export function getRandomEmoji() {
export function capitalize(str) {
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,
};