【问题标题】:How to delete messages and log them back in another channel如何删除消息并将它们记录回另一个频道
【发布时间】:2019-03-05 22:05:35
【问题描述】:

我想知道如何使用!clear [number] 之类的命令删除消息并将这些消息提取回一个频道,包括:

  • 使用命令删除消息的用户ID
  • 删除消息的用户 ID
  • 消息的内容
  • 它已被删除的频道
  • 消息的时间戳

所有这些东西都在一个不和谐的嵌入中。
我对编码比较陌生,我正在为一个有 40,000 人的服务器开发这个机器人,我们需要保留所有已删除消息的日志。

请有人帮帮我。我将不胜感激:D。如果需要,如果您仍然不确定我想用这个机器人做什么,我可以更详细地解释:D

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    要使用命令删除消息,您必须使用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);
        }
      }
    });
    

    注意:这种带有大量反引号字符串和对象的东西的高亮显示非常糟糕。我建议在另一个编辑器中阅读代码,否则您可能最终什么都不懂

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2021-12-06
      • 2021-08-17
      • 2021-04-01
      • 1970-01-01
      相关资源
      最近更新 更多