Alzalia
Alzalia
DIAdiscord.js - Imagine an app
Created by Alzalia on 8/11/2023 in #djs-questions
Edit a database when mute/warn is over
So, I did a command to mute (using timeout method, no problem, it works), same for warn (simple message). For each and every sanction made by a moderator, it's stored in a SQLite database with some useful infos. Now i can have the infos stored in the db, and the message of "warn/mute executed". What i'm stuck onto is how to make a long period timeout ? Say 3 months ? I tried, on ChatGPT recommandation (so like, worst idea I could have had but who knows) to use node-cron and 'events' so that an event is emitted, and another file receive it, and launch a cron.schedule to the said date, and then it executes the code of updating the db and sending a message saying the sanction is over. (Files in next message)
11 replies
DIAdiscord.js - Imagine an app
Created by Alzalia on 7/25/2023 in #djs-questions
Error after following the cooldown guide
Hi. New to discord.js, I thus followed the guide. Everything worked well, but I then arrived to the cooldown part. And now, I get this error when running node index.js :
node:events:489
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of undefined (reading 'has')
at Client.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\index.js:45:17)
at Client.emit (node:events:511:28)
at InteractionCreateAction.handle (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
at WebSocketManager.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
at WebSocketManager.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:1103:51)
at WebSocketShard.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.onMessage (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:938:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
node:events:489
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of undefined (reading 'has')
at Client.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\index.js:45:17)
at Client.emit (node:events:511:28)
at InteractionCreateAction.handle (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
at module.exports [as INTERACTION_CREATE] (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
at WebSocketManager.handlePacket (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
at WebSocketManager.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
at WebSocketManager.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.<anonymous> (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:1103:51)
at WebSocketShard.emit (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
at WebSocketShard.onMessage (E:\Desktop\vie reel\Elix_bee - stream\BeeBot\BeeBot_SourceCode\node_modules\@discordjs\ws\dist\index.js:938:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
And here is my client on event create part :
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

const { cooldowns } = client;

if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 2;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;

if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
}
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
client.on(Events.InteractionCreate, async interaction => {
if (!interaction.isChatInputCommand()) return;

const command = client.commands.get(interaction.commandName);

if (!command) return;

const { cooldowns } = client;

if (!cooldowns.has(command.data.name)) {
cooldowns.set(command.data.name, new Collection());
}

const now = Date.now();
const timestamps = cooldowns.get(command.data.name);
const defaultCooldownDuration = 2;
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;

if (timestamps.has(interaction.user.id)) {
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;

if (now < expirationTime) {
const expiredTimestamp = Math.round(expirationTime / 1000);
return interaction.reply({ content: `Please wait, you are on a cooldown for \`${command.data.name}\`. You can use it again <t:${expiredTimestamp}:R>.`, ephemeral: true });
}
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
});
But my code is basically the exact same as the guide one, so... I really don't get what happens ;-; Thank you for your time ^^
6 replies
DIAdiscord.js - Imagine an app
Created by Alzalia on 7/24/2023 in #djs-questions
Keep the bot online
So, this is probably a VERY stupid question. I have a VPS, opened an SSH session using PuTTy, and executed node index.js. The bot became online, and commands worked beautiful. So I closed PuTTy. But after a few minutes, the bot went offline again. I though it would be as simple as just running the file, and then go go bot, but apparently not. Do I need to install a program like pm2 ? (Because this programm was used for pcs where I found it, so I don't know) Thank you very much for your time !
4 replies