-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
112 lines (100 loc) · 3.67 KB
/
Copy pathbot.js
File metadata and controls
112 lines (100 loc) · 3.67 KB
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
108
109
110
111
112
require('dotenv').config();
const { Client, GatewayIntentBits, Collection } = require('discord.js');
const { deployCommands } = require('./deploy-commands');
const taskManager = require('./taskManager');
const reminderService = require('./reminderService');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
client.commands = new Collection();
// Load command handlers
const commands = require('./commands');
for (const [name, command] of Object.entries(commands)) {
client.commands.set(name, command);
}
client.once('ready', async () => {
console.log(`✅ TaskBot is online as ${client.user.tag}`);
await deployCommands(client);
reminderService.start(client);
});
client.on('interactionCreate', async (interaction) => {
try {
// Block DM usage
if (!interaction.guildId) {
if (interaction.isRepliable()) {
await interaction.reply({ content: '❌ This bot only works inside a server, not in DMs.', ephemeral: true });
}
return;
}
// Guild object is null when the bot was invited without the 'bot' scope.
if (!interaction.guild) {
if (interaction.isRepliable()) {
await interaction.reply({
content: '❌ Setup required: the bot must be re-invited with the **bot** scope and permissions. Ask a server admin to use the correct invite link.',
ephemeral: true,
});
}
return;
}
if (interaction.isChatInputCommand()) {
const command = client.commands.get(interaction.commandName);
if (command) await command.execute(interaction);
} else if (interaction.isButton()) {
await handleButton(interaction);
} else if (interaction.isModalSubmit()) {
await handleModal(interaction);
} else if (interaction.isStringSelectMenu()) {
await handleSelect(interaction);
} else if (interaction.isUserSelectMenu()) {
await handleUserSelect(interaction);
}
} catch (err) {
console.error('[interaction error]', interaction.commandName || interaction.customId, err);
const reply = { content: `❌ An error occurred: ${err.message}`, ephemeral: true };
if (interaction.replied || interaction.deferred) {
await interaction.followUp(reply).catch(() => {});
} else {
await interaction.reply(reply).catch(() => {});
}
}
});
async function handleButton(interaction) {
const [action, ...params] = interaction.customId.split(':');
const handlers = require('./buttonHandlers');
if (handlers[action]) {
await handlers[action](interaction, params);
} else {
await interaction.reply({ content: '❌ Unknown button action.', ephemeral: true });
}
}
async function handleModal(interaction) {
const [action, ...params] = interaction.customId.split(':');
const handlers = require('./modalHandlers');
if (handlers[action]) {
await handlers[action](interaction, params);
} else {
await interaction.reply({ content: '❌ Unknown form submission.', ephemeral: true });
}
}
async function handleSelect(interaction) {
const [action, ...params] = interaction.customId.split(':');
const handlers = require('./selectHandlers');
if (handlers[action]) {
await handlers[action](interaction, params);
} else {
await interaction.reply({ content: '❌ Unknown select action.', ephemeral: true });
}
}
async function handleUserSelect(interaction) {
const [action, ...params] = interaction.customId.split(':');
const handlers = require('./selectHandlers');
if (handlers[action]) {
await handlers[action](interaction, params);
} else {
await interaction.reply({ content: '❌ Unknown selection.', ephemeral: true });
}
}
client.login(process.env.DISCORD_TOKEN);