覆盖公会中每个频道的TextChannel权限

时间:2019-09-29 01:09:56

标签: discord.js

我目前正在为我的机器人开发一个静音功能,但到目前为止,它只为发送消息的通道设置了权限。

我尝试使用 guild.channels 属性,但是最终却一无所获,尽管我确定我只是在错误地使用它。

schema "messages" do
    belongs_to :user, User
end

我希望它为所有通道设置SEND_MESSAGES权限,但仅对我发送命令的通道设置。

1 个答案:

答案 0 :(得分:0)

Iterate over公会中的文本通道。

const channels = message.guild.channels.filter(c => c.type === 'text');

for (let [, channel] of channels) {
//        ^
// Ignoring the key; we don't need it
}

为我们提供for...of loop时为什么使用forEach()?后者只是简单地调用承诺并继续前进,而不能确保其完成。因此,如果发生错误,即使附加了catch()方法也不会捕获到该错误。


使用GuildChannel.overwritePermissions()覆盖每个频道的权限(注意:此方法将替换权限覆盖master分支)。

channel.overwritePermissions(msg.mentions.users.first(), { SEND_MESSAGES: false })
  .catch(console.error); // Catch and log errors

放在一起...

const channels = message.guild.channels;

for (let [, channel] of channels) {
  channel.overwritePermissions(msg.mentions.users.first(), { SEND_MESSAGES: false })
    .catch(console.error);
}
相关问题