discord bot ping pong test

This commit is contained in:
Kalle (Sendou)
2020-12-22 18:41:40 +02:00
parent a4c3faf1c8
commit 9da5075b30
4 changed files with 75 additions and 42 deletions

52
discord/index.ts Normal file
View File

@@ -0,0 +1,52 @@
import {
InteractionResponseFlags,
InteractionResponseType,
} from "discord-interactions";
type InvocationCommon = {
member: {
user: {
id: number;
username: string;
avatar: string;
discriminator: string;
public_flags: number;
};
roles: string[];
};
};
interface Interaction extends InvocationCommon {
data: { name: "ping" };
}
type ReturnData = {
type: InteractionResponseType;
data: {
content: string;
};
flags?: InteractionResponseFlags;
};
const handleCommand = (interaction: Interaction): ReturnData => {
switch (interaction.data.name) {
case "ping":
return {
type: InteractionResponseType.CHANNEL_MESSAGE,
data: {
content: "pong",
},
};
default:
console.error("not existing command invoced");
return {
type: InteractionResponseType.CHANNEL_MESSAGE,
data: {
content: "Sorry but I don't understand this command 😞",
},
flags: InteractionResponseFlags.EPHEMERAL,
};
}
};
export default handleCommand;

8
package-lock.json generated
View File

@@ -4429,6 +4429,14 @@
}
}
},
"discord-interactions": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/discord-interactions/-/discord-interactions-1.4.1.tgz",
"integrity": "sha512-JLB+uAWEluymJWlJkQ9GJui2cYdTMM3pGOEN5j3p2S/+7xdRjjGtqGgRGAqA5tyPVUHHchtsV/EAooVgM0cVBg==",
"requires": {
"tweetnacl": "^1.0.3"
}
},
"dom-helpers": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/dom-helpers/-/dom-helpers-3.4.0.tgz",

View File

@@ -32,6 +32,7 @@
"@prisma/client": "^2.13.1",
"@sendou/react-sketch": "^0.5.2",
"countries-list": "^2.5.6",
"discord-interactions": "^1.4.1",
"framer-motion": "^3.1.1",
"next": "^10.0.3",
"next-auth": "^3.1.0",

View File

@@ -1,43 +1,9 @@
import handleCommand from "discord";
import { InteractionType } from "discord-interactions";
import { NextApiRequest, NextApiResponse } from "next";
import getRawBody from "raw-body";
import nacl from "tweetnacl";
/*
{
"type": 2,
"token": "A_UNIQUE_TOKEN",
"member": {
"user": {
"id": 53908232506183680,
"username": "Mason",
"avatar": "a_d5efa99b3eeaa7dd43acca82f5692432",
"discriminator": "1337",
"public_flags": 131141
},
"roles": ["539082325061836999"],
"premium_since": null,
"permissions": "2147483647",
"pending": false,
"nick": null,
"mute": false,
"joined_at": "2017-03-13T19:19:14.040000+00:00",
"is_pending": false,
"deaf": false
},
"id": "786008729715212338",
"guild_id": "290926798626357999",
"data": {
"options": [{
"name": "cardname",
"value": "The Gitrog Monster"
}],
"name": "cardsearch",
"id": "771825006014889984"
},
"channel_id": "645027906669510667"
}
*/
const discordCommandHandler = async (
req: NextApiRequest,
res: NextApiResponse
@@ -47,13 +13,13 @@ const discordCommandHandler = async (
const signature = req.headers["x-signature-ed25519"] as string;
const timestamp = req.headers["x-signature-timestamp"] as string;
const body = (await getRawBody(req)).toString(); // rawBody is expected to be a string, not raw bytes
const rawBody = (await getRawBody(req)).toString(); // rawBody is expected to be a string, not raw bytes
let isVerified;
try {
isVerified = nacl.sign.detached.verify(
Buffer.from(timestamp + body),
Buffer.from(timestamp + rawBody),
Buffer.from(signature, "hex"),
Buffer.from(PUBLIC_KEY, "hex")
);
@@ -65,10 +31,16 @@ const discordCommandHandler = async (
return res.status(401).end("invalid request signature");
}
// if req.json type === 1
res.status(200).json({
type: 1,
});
const interaction = JSON.parse(rawBody);
if (interaction && interaction.type === InteractionType.COMMAND) {
const result = handleCommand(interaction);
res.status(200).json(result);
} else {
res.status(200).json({
type: 1,
});
}
};
export default discordCommandHandler;