【问题标题】:Bot doesn't add message member to the channel - Discord.JSBot 不向频道添加消息成员 - Discord.JS
【发布时间】:2020-10-07 06:51:21
【问题描述】:

所以,我目前正在为我的机器人开发一个“临时通道”模块。当某个等级的用户!newvc时,机器人会创建一个他们可以使用的私人语音频道,可以添加人,当所有人离开时,它会在一段时间后自动删除。

一切正常,但我发现了一个错误,我找不到它发生的原因。基本上,当您第一次使用该命令时,一切正常,通道已创建,您被添加并移至类别。但是,如果您再次使用它,假设一分钟后您不会被添加。该频道已创建,设置为私有,但您的 message.member 不会被添加。然后它又会又不会,你明白了吗?

老实说,我找不到它这样做的原因,我唯一能想到的就是与 Discord 的 API 有关。

这是我的代码

        let member = message.member
        user = member.user

            message.delete()
            message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{

                id: message.guild.id,
                deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']

            }]).then(channel => {

                channel.overwritePermissions(member, {
                    CONNECT: true,
                    USE_VAD: true,
                    PRIORITY_SPEAKER: true
                })

                channel.setParent('567718414454358026')

            })

                let privatevc = new Discord.RichEmbed()
                .setDescription(':white_check_mark: Successfully created a voice channel!')
                .setColor(config.green)

                message.channel.send({ embed: privatevc }).then(msg => msg.delete(10000))

仅供参考:我的 Discord.JS 版本是 11.4(由于工作没时间更新)

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    首先,前两行应改为:

    let member = message.member,
        user = message.author;
    // or
    const { member, author: user } = message;
    

    虽然这不是问题,但在严格模式下会导致错误,因为从技术上讲,user = member.user 前面没有变量关键字。如果您不打算更改变量的值,则应该尝试使用 const。请注意message.authormessage.member.user 相同。

    其次,不推荐在Guild#createChannel 中使用permissionOverwrites 参数(请参阅https://discord.js.org/#/docs/main/v11/class/Guild?scrollTo=createChannel)。我知道 Discord.JS 已经废除了很多东西,尽管他们说“已弃用”。尝试使用 typeOrOptions 参数来创建具有适当覆盖的通道。 这是我建议的代码:

    (async () => {
      message.delete();
      message.guild.createChannel(`⭐ ${message.author.username}'s Room`, {
        type: 'voice',
        parent: '567718414454358026',
        permissionOverwrites: [{
          id: message.guild.id, // @everyone has the ID of the guild
          deny: ['VIEW_CHANNEL', 'CONNECT'],
        }, {
          id: message.author.id, // attach the permission overrides for the user directly here
          allow: ['VIEW_CHANNEL', 'CONNECT', 'USE_VAD', 'PRIORITY_SPEAKER']
        }]
      });
      const embed = new Discord.RichEmbed()
                    .setDescription(':white_check_mark: Successfully created a voice channel!')
                    .setColor(config.green);
      const sentMessage = await message.channel.send(embed);
      sentMessage.delete(10 * 1000);
    })();
    

    【讨论】:

    • 收到此错误:TypeError: channelType.toUpperCase is not a function。你确定这适用于 Discord.js 11.4
    • 你的代码肯定有错误,因为上面发送的代码都不包含toUpperCase()函数调用
    • @Tenclea 但问题是,我在我的“测试”命令中使用了它,这只是一个普通文件并且得到了同样的错误。
    • 尝试在整个代码中搜索channelType.toUpperCase,然后尝试解决问题
    • 代码中没有任何内容会导致此问题。它仅与此代码建议一起出现。
    【解决方案2】:

    我发现了问题。基本上,因为用户是在创建频道之后添加的,Discord API 正在丢失它(或者其他什么,这些只是我的猜测)。

    改成这样后:

                message.guild.createChannel(`⭐${member.user.username}'s Room`, 'voice', [{
    
                    id: message.guild.id,
                    deny: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
    
                }, {
    
                    id: message.author.id,
                    allow: ['CONNECT', 'SPEAK', 'PRIORITY_SPEAKER']
    
                }])
    

    一切都恢复正常了。谢谢 PiggyPlex。

    【讨论】:

      猜你喜欢
      • 2021-01-10
      • 2022-01-05
      • 2020-10-08
      • 1970-01-01
      • 2021-08-21
      • 2020-04-04
      • 1970-01-01
      • 2016-04-24
      相关资源
      最近更新 更多