【问题标题】:How to store data for a user across servers discord如何跨服务器为用户存储数据不和谐
【发布时间】:2020-11-24 04:19:30
【问题描述】:

我在任何地方都找不到任何对我有帮助的答案,所以我想我会自己问。

您如何跨服务器存储一位用户的数据?示例:如果服务器 A 有一个机器人,并且用户输入了他们喜欢的号码或其他内容,那么服务器 B 上的同一个机器人如何打印该号码? (假设用户在两台服务器上)。 DM 与机器人的工作方式是否相同?多个用户呢?

我真的不知道你为此使用了什么类或变量,所以我自己没有尝试过任何东西。

【问题讨论】:

  • 客户端事件message 正在侦听来自您的机器人所在的所有服务器的消息。您可以使用message.guild 获取Guild

标签: javascript discord discord.js


【解决方案1】:

我已经编写了一些代码来接受命令!favNum!favColor。如果没有传递参数,机器人将尝试检索保存的值;如果传递了参数,机器人将保存该值。

消息事件处理程序侦听所有服务器上的消息,如Jakye in the comments 所述:

客户端事件message 正在侦听来自您的机器人所在的所有服务器的消息。您可以使用message.guild 获取Guild

警告:如果机器人重新启动,以下代码将丢失所有数据。

// 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\`.`);
});

【讨论】:

  • 会试试这个。我正在尝试先托管我的机器人
【解决方案2】:

您可以通过message.guild获取消息来自的公会,通过message.content获取消息的内容。

现在在您的具体示例中,它应该适用于以下内容:

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

【讨论】:

  • 如果用户在该频道中输入命令,获取频道 id 和guildB id 会更容易,这是我打算做的。
  • 但是对于每个用户都需要一个message.content 变量的多个用户呢?您将如何使用用户 ID 自动创建一个?有多少用户会让机器人无法处理这些变量?
  • 当然,使用命令而不是像我那样硬编码会更容易。但问题是关于 discord.js 中的对象和属性,而不是关于如何构建命令。
  • 如果你想用 DM 来做这件事,它会变得有点复杂。您可以使用client.users.cache.get("userID"); 找到您想要接收消息的用户 机器人需要至少有一个与用户共同的服务器才能发送直接消息。因此,请确保您的机器人位于用户的服务器中。
  • 我不明白你打算做什么。因此,您正在尝试使用用户和来自其他频道的频道 ID 作为参数来构建命令?
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-07
  • 2020-10-20
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
相关资源
最近更新 更多