I’m currently developing a Discord bot in JavaScript and I’m learning more about its functionalities. My question revolves around how the bot processes various events happening in different servers, given that JS is single-threaded. Specifically, how are event-related variables managed? If two identical events occur in separate guilds, do the values of their variables interfere with each other?
Here’s a relevant code snippet that may help clarify my question:
const Discord = require('discord.js');
const bot = new Discord.Client();
const botToken = 'insert_token_here';
bot.on('ready', () => {
console.log('Bot is online!');
});
bot.on('message', receivedMessage => {
if (receivedMessage.content.startsWith('bot!')) {
let command = receivedMessage.content.substring(4);
if (command.includes('(')) {
switch (command.substring(0, command.indexOf('('))) {
case 'list':
console.log(bot.guilds);
bot.guilds.cache.forEach(guild => {
console.log(guild.name);
});
break;
}
} else {
switch (command) {
case 'status':
receivedMessage.channel.send('Bot is active!');
break;
case 'shutdown':
receivedMessage.channel.send("Going offline").then(() => {
bot.destroy();
process.exit();
});
break;
}
}
}
});
bot.login(botToken);