How to manage a Discord bot across multiple servers?

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);

JavaScript’s single-threaded nature can indeed raise some concerns when it comes to handling events in a multi-guild environment, but rest assured, Discord.js is designed to handle such operations smoothly. The event loop in Node.js allows your bot to process each event independently. When an event, like a message from a different server, is triggered, it doesn’t really “pause” the bot globally. Instead, each event is isolated in the sense that they don’t interfere with each other’s data. Every time a message event is triggered, the related function is executed with its specific context, meaning your variables within the event handler are unique to that particular invocation. So, you don’t have to worry about variable “cross-traffic” between guilds since each call gets its own execution frame.