user14031530
user14031530

Reputation:

How to store data for a user across servers discord

I couldn't find any answers that helped me anywhere so I guess i'll ask it myself.

How do you store data for one user across servers? Example: If server A has a bot and the user enters their favorite number or something, how can the same bot on server B print that number? (Assuming the user is on both servers). And does it work the same way with DM's with the bot? What about multiple users?

I really have no clue what class or variable you use for this, so I haven't tried anything myself.

Upvotes: 1

Views: 867

Answers (2)

Daemon Beast
Daemon Beast

Reputation: 2909

I have made some code to accept the commands !favNum and !favColor. If no arguments are passed, the bot will attempt to retrieve the saved value; if an argument is passed, the bot will save that value.

The message event handler listens for messages on all servers, as stated by Jakye in the comments:

The client's event, message is listening for messages from all servers your bot is in. You can use message.guild to get the Guild.

Warning: the following code will lose all data if the bot restarts.

// Set a prefix for commands
const prefix = '!';

// Create a collection for all data
const data = new Discord.Collection();
// Create individual collections for different pieces of data
data.set('favNum', new Discord.Collection());
data.set('favColor', new Discord.Collection());

client.on('message', message => {
  // Exit if the message was sent by a bot or doesn't start with the command prefix
  if (message.author.bot || !message.content.startsWith(prefix)) return;

  // Split the message into a command and arguments
  const args = message.content.slice(prefix.length).trim().split(/\s+/g);
  const command = args.shift().toLowerCase();

  // Execute a section of code depending on what the command was
  switch (command) {
    // Favourite number command
    case 'favNum':
      // If no arguments were passed, try to get the saved value
      if (!args[0]) {
        if (!data.get('favNum').has(message.author.id)) return message.reply(`you haven't set a favorite number.`);

        const favNum = data.get('favNum').get(message.author.id);
        message.reply(`your favorite number is ${favNum}.`);
      }

      // If arguments were passed, set the value
      data.get('favNum').set(message.author.id, args[0]);
      break;

    // Favourite color command
    case 'favColor':
      // If no arguments were passed, try to get the saved value
      if (!data.get('favColor').has(message.author.id)) return message.reply(`you haven't set a favorite color.`);

        const favColor = data.get('favColor').get(message.author.id);
        message.reply(`your favorite color is ${favColor}.`);
      }

      // If arguments were passed, set the value
      data.get('favColor').set(message.author.id, args[0]);
      break;

    // Default reply if the command was not recognized
    default:
      message.reply(`could not recognize the command \`command\`.`);
});

Upvotes: 1

Behemoth
Behemoth

Reputation: 9350

You can get the guild where the message came from with message.guild and the content of the message with message.content.

Now in your specific example, it should work with something like the following:

client.on("message", message => {
   const messageContent = message.content;
   const guildA = message.guild;
   const guildB = client.guilds.cache.get("the id of guild B"); //you can get this with guild.id
   
   //Now we just need to get the channel where the message in guild B should appear
   //This would be one way of doing it:
   const channel = guildB.channels.cache.get("the id of some channel of guildB");

   channel.send(messageContent);
}

Upvotes: 0

Related Questions