简体   繁体   中英

discord.js message.channel is not a function

I am trying to make my bot delete messages in a specific channel that are not images.

client.on("message", (message) => {
  let channel = client.channels.get(`642417479708049418`);
  if(!message.channel(channel)) {
    return
  } else {
  if(message.embeds.length > 0) {
    return
  } else {
    message.delete();
    message.channel.send("images only");
  }
}
});

the error says that message.channel is not a function.

The error is in .message.channel(channel) , to verify if the channel is the one you want it to be, since you already have the channel variable, just compare the two ids, you should also add a check to see if the message is coming from a bot or not, so that the bot doesn't end up in a loop of deleting its own messages:

client.on("message", (message) => {
    if (message.author.bot) return;

    let channel = client.channels.get(`642417479708049418`);
    if (message.channel.id != channel.id) {
        return
    } else {
        if (message.embeds.length > 0) {
            return
        } else {
            message.channel.send("images only");
            message.delete();
        }
    }
});

Also when you delete the message in message.delete(); be sure to send the response you want to send first so do it in this order:

message.channel.send("images only");
message.delete();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM