无法让自动断开机器人断开已经在语音中的用户

我目前正在尝试运行一个不和谐的机器人,它会在特定用户尝试进入时断开我服务器上 afk 频道的连接。


诚然,我不太擅长编码,而且我所做的大部分工作都是从我浏览过的其他帮助部分拼凑而成的。


我已经设法让机器人进入这样的状态,当用户直接进入 afk 频道时,它会断开用户的连接,但我无法弄清楚当用户离开时如何让它做同样的事情一个频道到 afk 频道。


我遇到问题的部分在这里:


client.on("voiceStateUpdate", (oldMember, newMember) => {

  const newUserChannel = newMember.voiceChannel;

  const oldUserChannel = oldMember.voiceChannel;


  if (

    oldUserChannel === undefined &&

    newUserChannel.name === "afk" &&

    data.find(

      guild =>

        guild.id === newMember.guild.id &&

        guild.blacklist.find(id => newMember.id === id)

    )

  ) {

    newMember.setVoiceChannel(null);

  }

我已经尝试设置oldUserChannel === undefined &&为特定频道,并且 !== undefined,但是除了 === undefined 以外的任何东西都会给我一个 TypeError: Cannot read property 'name' of undefined。


我也尝试过使用 usernewUserChannel.id而不是 .name,但这只会给我带来与“name”更改为“id”相同的错误。


我对此真的很陌生,所以我正在努力想我能做些什么来修复这个错误。


临摹微笑
浏览 84回答 1
1回答

白板的微信

如果用户正AFK从另一个频道移动到该频道,oldUserChannel将被定义为VoiceChannel.您应该检查是否newUserChannel存在(如果是这样,我们知道用户仍然连接到 a VoiceChannel,并检查频道的 ID/名称是否等于“AFK”/“频道 ID”。注意:我鼓励你切换到 Discord JS v12。client.on("voiceStateUpdate", (oldMember, newMember) => {    if (!newMember.voiceChannel) return false; // The GuildMember is not in a VoiceChannel.    if (newMember.voiceChannel.guild.id !== "GUILD ID") return false; // Making sure this works in a certain Guild. I don't think you want this to happen in every Guild your bot is in which has a VoiceChannel called "AFK".     if (newMember.voiceChannel.name == "AFK" && newMember.id == "USER ID") { // Checking if the channel's name is AFK and making sure the user is the one you want to disconnect.        newMember.setVoiceChannel(null);        // I'm not sure if Discord JS v11 has a method of disconnecting the user from a VoiceChannel.        // But setting the voice channel to null will work.    };});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript