【问题标题】:discord.js deleting only user messages and botdiscord.js 仅删除用户消息和机器人
【发布时间】:2021-08-06 11:48:48
【问题描述】:

我想让我的机器人只删除某个频道中用户的消息,而不是机器人的消息。我尝试使用下面的代码执行此操作,但它不断删除机器人的消息和我的消息。

const Discord = require("discord.js");
const client = new Discord.Client();
const { MessageEmbed } = require("discord.js");
const avalibleFormats = ['png', 'gif', 'jpeg', 'jpg']
 
client.on("ready", () => {
  console.log("I am ready!");
});
 
client.on("message", message => {
    if (message.channel.id == '829616433985486848') {
        message.delete();
      }
    if (message.channel.id !== '829616433985486848') {
        return;
    }

    let image = getImage(message)
    if (!image) {
        return;
    }

    let embed = new MessageEmbed();
    embed.setImage(image.url)
    embed.setColor(`#2f3136`)
    message.channel.send(embed)

});
const getImage = (message) => message.attachments.find(attachment => checkFormat(attachment.url))
const checkFormat = (url) => avalibleFormats.some(format => url.endsWith(format))
 
client.login(token);

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    好吧,你只是说如果频道 id 是829616433985486848,则删除该消息。您还应该使用message.author.bot 属性检查author 是否为bot

    const avalibleFormats = ['png', 'gif', 'jpeg', 'jpg'];
    
    const checkFormat = (url) => avalibleFormats.some((format) => url.endsWith(format));
    const getImage = (message) => message.attachments.find((attachment) => checkFormat(attachment.url));
    
    client.on('message', (message) => {
      const certainChannelId = '829616433985486848';
    
      // if the channel is not 829616433985486848, return to exit
      if (message.channel.id !== certainChannelId)
        return;
    
      // the rest of the code only runs if the channel is 829616433985486848
    
      const image = getImage(message);
    
      // if author is not a bot, delete the message
      if (!message.author.bot)
        message.delete();
    
      if (!image)
        return;
    
      const embed = new MessageEmbed()
        .setImage(image.url)
        .setColor('#2f3136');
    
      message.channel.send(embed);
    });
    

    实际上,如果消息是由机器人发布的,您甚至不需要在其中运行任何东西,因此您可以在开始时检查并提前退出:

    client.on('message', (message) => {
      if (message.author.bot || message.channel.id !== '829616433985486848')
        return;
    
      const image = getImage(message);
    
      if (image) {
        const embed = new MessageEmbed()
          .setImage(image.url)
          .setColor('#2f3136');
    
        message.channel.send(embed);
      }
      message.delete();
    });
    

    如果您希望它在多个频道中工作,您可以创建一个频道 ID 数组并使用 Array#includes() 检查当前频道 ID 是否在该数组中:

    client.on('message', (message) => {
      const channelIDs = ['829616433985486848', '829616433985480120', '829616433985485571'];
      if (message.author.bot || !channelIDs.includes(message.channel.id))
        return;
    
      const image = getImage(message);
    
      if (image) {
        const embed = new MessageEmbed()
          .setImage(image.url)
          .setColor('#2f3136');
    
        message.channel.send(embed);
      }
      message.delete();
    });
    

    【讨论】:

    • 我不知道你为什么添加它,但你需要删除if (message.channel.id !== channelIDs) return;。这总是错误的......我再次更新了我的答案,以包括处理多个渠道的整个client.on('message') 部分。
    猜你喜欢
    • 2021-08-19
    • 2020-11-02
    • 2021-07-27
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 2020-10-26
    • 2020-11-10
    • 2018-06-05
    相关资源
    最近更新 更多