如何删除邮件并将其重新登录到另一个频道

时间:2018-09-30 17:09:42

标签: javascript discord discord.js

我想知道如何使用!clear [number]之类的命令删除邮件,并将这些邮件取回包含以下内容的频道:

  • 使用该命令删除消息的用户ID
  • 说删除邮件的人的用户ID
  • 邮件内容
  • 频道已从中删除
  • 消息的时间戳

所有这些内容都以不和谐的形式嵌入。
我是编码的新手,我正在为拥有40,000人的服务器开发此bot,我们需要保留所有已删除邮件的日志。

请有人帮帮我。我将不胜感激:D。如果需要,我可以进一步详细说明您是否仍然不确定我要如何使用此机器人:D

1 个答案:

答案 0 :(得分:0)

要使用命令删除消息,您必须使用TextChannel.bulkDelete(),在这种情况下,可能是通过TextChannel.fetchMessages()获取消息之后。
要“记录”它们,您可能需要构建一个RichEmbed并将您的信息放入字段中。
我会尝试这样的事情:

// ASSUMPTIONS:
// logs = the TextChannel in wich you want the embed to be sent
// trigger = the Messages that triggered the command
// 
// This is just a sample implementation, it could contain errors or might not be the fastest
// Its aim is not to make it for you, but to give you a model

async function clear(n = 1, logs, trigger) {
  let {channel: source, author} = trigger;
  if (n < 1 || !logs || !source) throw new Error("One of the arguments was wrong.");

  let coll = await source.fetchMessages({ limit: n }), // get the messages
    arr = coll.array(),
    collected = [],
    embeds = [];

  // create groups of 25 messages, the field limit for a RichEmbed
  let index = 0;
  for (let i = 0; i < arr.length; i += 25) {
    collected.push([]);
    for (let m = i; m < i + 25; m++)
      if (arr[m]) collected[index].push(arr[m]);
    index++;
  }

  // for every group of messages, create an embed
  // I used some sample titles that you can obviously modify
  for (let i = 0; i < collected.length; i++) {
    let embed = new Discord.RichEmbed()
      .setTitle(`Channel cleaning${collected.length > 1 ? ` - Part ${i+1}` : ""}`)
      .setDescription(`Deleted from ${source}`)
      .setAuthor(`${author.tag} (${author.id})`, author.displayAvatarURL)
      .setTimestamp(trigger.editedAt ? trigger.editedAt : trigger.createdAt),
      group = collected[i];
    for (let msg of group) {
      let a = `${msg.author.tag} (${msg.author.id}) at ${msg.editedAt ? msg.editedAt : msg.createdAt}`,
        c = msg.content.length > 1024 ? msg.content.substring(0, msg.content.length - 3) + '...' : msg.content;
      embed.addField(a, c);
    }
    embeds.push(embed);
  }

  // once the embeds are ready, you can send them
  source.bulkDelete(coll).then(async () => {
    for (let embed of embeds) await source.send({embed});
  }).catch(console.error);
}

// this is a REALLY basic command implementation, please DO NOT use this as yours
client.on('message', async msg => {
  let command = 'clear ';
  if (msg.content.startsWith(command)) {
    let args = msg.content.substring(command.length).split(' ');
    if (isNaN(args[0]) || parseInt(args[0]) < 1) msg.reply(`\`${args[0]}\` is not a valid number.`);
    else {
      await msg.delete(); // delete your message first
      let logs; // store your channel here
      clear(parseInt(args[0]), logs, msg);
    }
  }
});

注意:此类内容的突出显示是非常糟糕的,其中包含很多反引号字符串和对象。我建议在其他编辑器中阅读代码,否则您可能最终不了解任何内容