-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (63 loc) · 1.91 KB
/
index.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
const {
Client,
EmbedBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle
} = require("discord.js");
const prettyMs = require("pretty-ms");
const { QuickDB } = require("quick.db");
const { getRandomFromArray } = require("./random.js");
const { cooldown, sendChannel, token, embed, buttons } = require("./config.json");
const db = new QuickDB();
const client = new Client({ intents: [] });
client.on("ready", async () => {
console.log(`Bot ${client.user.username} is ready`);
if (sendChannel) {
let channel;
try {
channel = await client.channels.fetch(sendChannel);
} catch {
throw new Error(`Channel with ID ${sendChannel} not found`);
}
channel?.send({
embeds: [
EmbedBuilder.from(embed)
],
components: [
new ActionRowBuilder()
.addComponents(
buttons.map(button =>
new ButtonBuilder()
.setLabel(button.label)
.setCustomId(button.id)
.setStyle(ButtonStyle.Primary)
)
)
]
});
}
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isButton()) return;
const userData = await db.get(`user_${interaction.user.id}`) ?? {};
const waitFor = (
userData.lastClicked ?
Math.max(cooldown - (Date.now() - userData.lastClicked), 0) :
0
);
if (waitFor > 0) return await interaction.reply({
content: `You've already clicked a button recently! Please wait **${prettyMs(waitFor, { unitCount: 2 })}** before clicking one again.`,
ephemeral: true
});
const button = buttons.find(button => button.id === interaction.customId);
if (!button) return;
userData.lastClicked = Date.now();
await db.set(`user_${interaction.user.id}`, userData);
const { responses } = button;
await interaction.reply({
content: getRandomFromArray(responses),
ephemeral: true
});
});
client.login(token);