【问题标题】:Making your discord bot save messages that you sent让您的不和谐机器人保存您发送的消息
【发布时间】:2020-09-13 08:11:06
【问题描述】:

我正在开发我的不和谐机器人,我希望它保存我发送给它的消息,我该怎么做,因为互联网的其他部分出于某种原因不会问这个问题。我一直在找人指点我的方向,但没有找到任何东西

【问题讨论】:

  • 您是否正在尝试记录用户在您的机器人所在的服务器中发送的消息?
  • 不是真的,我试图让它当你说 +add "hello" 它添加到一个列表中,所以当你输入 +show 它会显示你添加的单词
  • 也许像创建一个数组,然后将 args[1] 添加到数组中。所以 +add "hello" 会将 "hello" 添加到数组中。当用户执行 +show 时,只需发送数组中的项目。

标签: javascript json bots discord discord.js


【解决方案1】:

这是您想要做的真正简化的版本,但我相信如果您阅读了它,您就会理解并完成工作。

const discord = require('discord.js'); // Import discord.js

const client = new discord.Client(); // Initialize new discord.js client

const messages = [];

client.on('message', (msg) => {
  if (!msg.content.startsWith('+')) return; // If the message doesn't start with the prefix return

  const args = msg.content.slice(1).split(' '); // Split all spaces so you can get the command out of the message
  const cmd = args.shift().toLowerCase(); // Get the commands name

  switch (cmd) {
    case 'add':
      messages.push(args.join(' ')); // Add the message's content to the messages array
      break;
    case 'get':
      msg.channel.send(messages.map((message) => `${message}\n`)); /* Get all the stored messages and map them + send them */
      break;
  }
});

client.login(/* Your token */); // Login discord.js

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 1970-01-01
    • 2022-11-02
    • 2020-12-26
    • 2019-07-11
    • 2021-08-28
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多