【问题标题】:Discord Bot who disconnected a user from voice channelDiscord Bot 将用户与语音通道断开连接
【发布时间】:2021-07-01 18:44:14
【问题描述】:

我正在尝试让我的机器人在有人断开连接时发送消息。其中包含断开连接的人和断开连接的人。

但是当我运行机器人并断开某人的连接时,什么都没有发生。

这是我的代码:

client.on("voiceStateUpdate", function (oldMember, newMember) {
  let newUserChannel = newMember.voiceChannel

  if (newUserChannel === null) {

    // User leaves a voice channel

    const fetchedLogs = await (oldMember, newMember).guild.fetchAuditLogs({
        limit: 1,
        type: 'MEMBER_DISCONNECT',
    });

    const disconnectLog = fetchedLogs.entries.first();

    const { executor } = disconnectLog;

    client.channels.cache.get("828731501016252417").send(`<@${executor.id}> Disconnected <@${oldMember.id}>`)
}
});

【问题讨论】:

    标签: javascript node.js discord discord.js bots


    【解决方案1】:

    voiceStateUpdate 事件有VoiceState 类型参数,而不是成员。

    client.on("voiceStateUpdate", function (oldVoiceState, newVoiceState) {
      let newUserChannel = newVoiceState.channel
    
      if (newUserChannel === null) {
    
        // User leaves a voice channel
    
        const fetchedLogs = await (oldVoiceState, newVoiceState).guild.fetchAuditLogs({
            limit: 1,
            type: 'MEMBER_DISCONNECT',
        });
    
        const disconnectLog = fetchedLogs.entries.first();
    
        const { executor } = disconnectLog.executor;
    
        client.channels.cache.get("828731501016252417").send(`<@${executor.id}> Disconnected <@${oldVoiceState.id}>`)
    }
    });
    

    【讨论】:

    • 当我尝试断开 somone 时,我收到了 const fetchedLogs = await (oldVoiceState, newVoiceState).guild.fetchAuditLogs({ ^ ReferenceError: await is not defined
    • 我尝试添加async 而不是function,但我得到TypeError: Cannot read property 'id' of undefined
    【解决方案2】:

    没有发生任何事情的原因是 client.voiceStateUpdate 事件现在发出 oldStatenewState 代表成员的 VoiceStates 而不是成员本身。这是changed in discord.js v12。由于VoiceState 没有voiceChannel 属性,您的newUserChannel 将是undefined

    您的 if 语句 (if (newUserChannel === null)) 将始终为 false,因为 undefined 并不严格等于 null,因此其中的任何内容都不会被执行。

    您可以检查 newVoiceState.channel 是否为空。在尝试获取日志之前,您还应该检查机器人是否具有所需的VIEW_AUDIT_LOG 权限。您还错过了回调函数前面的async 关键字,您只能在异步函数中使用await

    检查下面的代码,它应该可以按预期工作。

    client.on('voiceStateUpdate', async (oldVoiceState, newVoiceState) => {
      // User leaves a voice channel
      if (newVoiceState.channel === null) {
        // check if the bot can view audit logs
        if (!oldVoiceState.guild.me.hasPermission('VIEW_AUDIT_LOG'))
          return console.log('Missing permission: "VIEW_AUDIT_LOG"');
    
        const fetchedLogs = await oldVoiceState.guild.fetchAuditLogs({
          limit: 1,
          type: 'MEMBER_DISCONNECT',
        });
    
        const { executor } = fetchedLogs.entries.first();
    
        client.channels.cache
          .get('828731501016252417')
          .send(`${executor} disconnected ${oldVoiceState.member}.`);
      }
    });
    

    PS: 每次有人离开语音频道时,当前函数都会发送最后一个条目,即使他们是自愿离开的。我认为,MEMBER_DISCONNECT 只会在其他人将成员从频道中踢出时才会被记录。您需要以某种方式验证最后一个日志条目是否与当前的 voiceStateUpdate 相同。

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2021-04-29
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-19
      • 2022-01-05
      相关资源
      最近更新 更多