让您的Discord机器人保存您发送的消息

时间:2020-05-26 13:45:18

标签: javascript json bots discord discord.js

我正在使用Discord机器人,我希望它保存发送给它的消息以进行保存,但由于互联网的其他部分出于某种原因没有问这个问题,我该怎么做。我一直在寻找可以指引我方向的人,但是什么也没找到

1 个答案:

答案 0 :(得分: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