最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Discord.js: message.guild.channels.forEach is not a function - Stack Overflow

programmeradmin10浏览0评论

I'm creating a Discord Bot using Discord.js

I'm creating a mute mand but when I want to disable speaking permission for the Mute role for each channel, I get this error:

TypeError: message.guild.channels.forEach is not a function

I have V12. And I looked at some other options but I couldn't find any working options.

  if(!toMute) return message.reply('It looks like you didnt specify the user!');
  if(toMute.hasPermission('MANAGE_MESSAGES')) return message.reply("can't mute them");
  let muterole = message.guild.roles.cache.find(r => r.name === 'muted');
  if(!muterole){
    try{
      muterole = await message.guild.roles.create({
        name: "muted",
        color: "#000000",
        permissions: []
      })
      message.guild.channels.forEach(async (channel, id) => {
        await channel.overwritePermission(muterole, {
          SEND_MESSAGES: false,
          ADD_REACTIONS: false
        });
      });
    }catch(e){
      console.log(e.stack);
    }
  } return message.channel.send('Cant')


  let mutetime = args[1];
  if(!mutetime) return message.reply('You didnt specify the time');

  await(toMute.addRole(muterole.id));
  message.reply(`Successfully muted <@${toMute.id}> for ${ms(mutetime)}`);

  setTimeout(function(){
    toMute.removeRole(muterole.id);
    message.channel.send(`<@${toMute.id}> has been unmuted!`);
  }, ms(mutetime));

}

I'm creating a Discord Bot using Discord.js

I'm creating a mute mand but when I want to disable speaking permission for the Mute role for each channel, I get this error:

TypeError: message.guild.channels.forEach is not a function

I have V12. And I looked at some other options but I couldn't find any working options.

  if(!toMute) return message.reply('It looks like you didnt specify the user!');
  if(toMute.hasPermission('MANAGE_MESSAGES')) return message.reply("can't mute them");
  let muterole = message.guild.roles.cache.find(r => r.name === 'muted');
  if(!muterole){
    try{
      muterole = await message.guild.roles.create({
        name: "muted",
        color: "#000000",
        permissions: []
      })
      message.guild.channels.forEach(async (channel, id) => {
        await channel.overwritePermission(muterole, {
          SEND_MESSAGES: false,
          ADD_REACTIONS: false
        });
      });
    }catch(e){
      console.log(e.stack);
    }
  } return message.channel.send('Cant')


  let mutetime = args[1];
  if(!mutetime) return message.reply('You didnt specify the time');

  await(toMute.addRole(muterole.id));
  message.reply(`Successfully muted <@${toMute.id}> for ${ms(mutetime)}`);

  setTimeout(function(){
    toMute.removeRole(muterole.id);
    message.channel.send(`<@${toMute.id}> has been unmuted!`);
  }, ms(mutetime));

}
Share Improve this question edited Apr 15, 2020 at 1:21 devcass 8016 silver badges16 bronze badges asked Apr 15, 2020 at 0:27 KivashoKivasho 11 gold badge1 silver badge1 bronze badge
Add a ment  | 

2 Answers 2

Reset to default 3

Please try

message.guild.channels.cache.forEach((channel)=>{
 ...
})

Reference: https://discord.js/#/docs/main/stable/class/GuildChannelManager?scrollTo=cache

It's like the error says. message.guild.channels.forEach is not a function!

You're probably using discord.js v12. In this version, message.guild.channels doesn't return a collection, it returns the ChannelManager. To get a collection of all channels, you use message.guild.channels.cache. And now you can use .forEach():

message.guild.channels.cache.forEach((channel) => {

 // your code here

});
发布评论

评论列表(0)

  1. 暂无评论