-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (64 loc) · 2.07 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const express = require("express");
const { Collection } = require("discord.js");
const { setTimeout } = require("node:timers/promises");
const {
InteractionType,
InteractionResponseType,
InteractionResponseFlags,
MessageComponentTypes,
ButtonStyleTypes,
verifyKeyMiddleware,
} = require("discord-interactions");
const port = process.env.PORT;
const Client = {};
Client.capitalize = async (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
Client.commands = new Collection();
Client.buttons = new Collection();
Client.modals = new Collection();
Client.wait = setTimeout;
Client.getOption = (interaction, option) => {
return interaction.data.options[0].options.filter(
(boo) => boo.name === option
)[0];
};
Client.getSubCommandName = (interaction) => {
return interaction.data.options[0].name;
};
// Create an express app
const app = express();
require("./database/mongoose.js");
require("./commands.js")(Client);
app.post("/interactions", verifyKeyMiddleware("SECRET"), (req, res) => {
const startAt = Date.now();
const interaction = req.body;
if (interaction.type === InteractionType.APPLICATION_COMMAND) {
const cmd = Client.commands.get(interaction.data.name);
console.log(interaction);
if (!cmd) {
res.send({
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
data: {
content: `Can't find this Command. :(`,
flags: InteractionResponseFlags.EPHEMERAL,
},
});
}
cmd.run(Client, interaction, res, startAt);
} else if (interaction.type === InteractionType.MODAL_SUBMIT) {
const modal = Client.modals.get(interaction.data.name);
if (!modal) return;
console.log(interaction);
modal.run(Client, interaction, res, startAt);
} else if (interaction.type === InteractionType.MESSAGE_COMPONENT) {
const button = Client.buttons.get(interaction.data.name);
if (!button) return;
console.log(interaction);
button.run(Client, interaction, res, startAt);
}
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
module.exports = app;