【问题标题】:Overwriting TextChannel permissions for every channel in a guild覆盖公会中每个频道的 TextChannel 权限
【发布时间】:2020-01-28 18:38:06
【问题描述】:

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

我尝试使用 guild.channels 属性,但最终无济于事,尽管我确信我只是用错了。

var timer = args[1]
var channels = msg.channel

channels.overwritePermissions(msg.mentions.users.first(), { SEND_MESSAGES: false });

msg.reply(`Muted ${msg.mentions.users.first().username} for ${timer} seconds`)

setTimeout(unmute, timer * 1000);

function unmute(){
  channels.overwritePermissions(msg.mentions.users.first(), { SEND_MESSAGES: true });
}

我希望它为所有频道设置 SEND_MESSAGES 权限,但它只对我发送命令的频道这样做。

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    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
    }
    

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


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

    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);
    }
    

    【讨论】:

    • 谢谢!对于自动取消静音,我最终将 SEND_MESSAGES 设置为 null,这样人们就不会输入我的公告。
    猜你喜欢
    • 2018-12-21
    • 2021-02-01
    • 2019-10-21
    • 2021-09-19
    • 2021-08-04
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多