This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbot.js
107 lines (83 loc) · 3.5 KB
/
bot.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import config from './config.json' assert { type: 'json' };
import helper from './helper.js';
import Keyv from 'keyv';
import fs from 'fs/promises';
import { Client, Intents } from 'discord.js';
import { parentPort } from 'worker_threads';
const db = new Keyv(config.dbUri, { namespace: config.dbNamespace });
helper.init(db);
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const commands = [];
const commandFiles = (await fs.readdir('./commands')).filter(file => file.endsWith('.js'));
async function reloadCommands() {
while(commands.length > 0) {
commands.pop();
}
for (const file of commandFiles) {
const { default: Command } = await import(`./commands/${file}?${Date.now()}`);
commands.push(new Command(client, helper, db));
}
}
client.on('interactionCreate', async interaction => {
try {
if (interaction.isButton()) {
const oInteraction = interaction.message.interaction;
if (interaction.user.id != oInteraction.user.id)
return;
const command = commands.find(c => c.command == oInteraction.commandName);
if (command == null)
return;
if (command.interact !== 'function')
return;
const extendedLayout = await helper.extendedLayout(interaction);
command.interact({ interaction, extendedLayout, client, db }).catch(console.error);
} else if (interaction.isCommand()) {
if (interaction.commandName == 'reload'
&& config.owners.includes(interaction.user.id)) {
try {
await client.shard.send('reloadCommands');
interaction.reply('Successfully reloaded commands!', { ephemeral: true }).catch(helper.error);
} catch(e) {
helper.error(e);
interaction.reply('Failed to reload commands', { ephemeral: true }).catch(helper.error);
}
client.shard.send('reloadCommands').catch(helper.error);
return;
}
const command = commands.find(a => a.command == interaction.commandName);
if (command == null)
return;
const extendedLayout = await helper.extendedLayout(interaction);
command.call({ interaction, extendedLayout, client, commands, db }).catch(err => {
interaction.reply({
embeds: [
{
color: helper.errorColor,
author: {
name: 'Error'
},
description: err.message
}
],
ephemeral: true
}).catch(() => {}) // we tried
});
} else if (interaction.isAutocomplete()) {
const command = commands.find(a => a.command == interaction.commandName);
if (command == null)
return;
if (typeof command.autocomplete !== 'function')
return;
command.autocomplete({ interaction, client }).catch(console.error);
}
} catch(e) {
helper.error(e);
}
});
parentPort.on('message', message => {
if (message === 'reloadCommands') {
reloadCommands().catch(helper.error);
}
});
reloadCommands().catch(helper.error);
client.login(config.credentials.bot_token);