【问题标题】:Discord - Sorting through members connected to a VC and giving them rolesDiscord - 对连接到 VC 的成员进行排序并赋予他们角色
【发布时间】:2021-01-12 13:58:33
【问题描述】:

我正在尝试通过我的 discord vc“Among Us”并给那里的每个人“游戏中”的角色。目前我的代码是:

bot.on('message', message => {
    //let role = member.guild.roles.cache.find(role => role.name === "In-Game");
    let role = message.guild.roles.cache.find(role => role.name == "In-Game");
    let channels = message.guild.channels.cache.filter(c => c.parentID === '759363400253308948' && c.type === 'voice');

    if (message.content.startsWith('.rank')) {
        for (const [channelID, channel] of channels) {
            for (const [memberID, member] of channel.members) {
                member.roles.add(role);
            }
        }
    }
})

我没有收到错误,但我也没有得到任何结果?为什么会这样?

【问题讨论】:

    标签: node.js discord bots


    【解决方案1】:

    我认为问题在于您的通道变量是空的,因此当您尝试循环它并遍历该通道中的成员时,什么也没有发生。 我认为这样做的原因是您的箭头函数,它过滤了某个父级的子频道,没有返回任何内容。 channel.parentID 返回一个数字,但您将它与数字字符串进行比较,使用检查值和类型的===。更改为 ==(它将检查相等的值,即使类型不同)或删除引号。

    bot.on('message', message => {
        //let role = member.guild.roles.cache.find(role => role.name === "In-Game");
        let role = message.guild.roles.cache.find(role => role.name == "In-Game");
        let channels = message.guild.channels.cache.filter(c => c.parentID == '759363400253308948' && c.type === 'voice');
    
        if (message.content.startsWith('.rank')) {
            for (const [channelID, channel] of channels) {
                for (const [memberID, member] of channel.members) {
                    member.roles.add(role);
                    //if you want to do anything after the roles have been added, you need to await the above line
                    //and make the event function async.
                }
            }
        }
    })
    

    注意:您的机器人将需要相关权限。我认为是必要的:
    阅读文本频道/查看语音频道:查看用户所在的频道
    管理角色:让您分配角色给用户。

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 2021-03-26
      • 2020-08-25
      • 2021-06-16
      • 2021-08-09
      • 2020-02-26
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多