【问题标题】:Sending a message to specific channel when user joins voice channel doesnt work用户加入语音频道时向特定频道发送消息不起作用
【发布时间】:2021-02-28 01:39:06
【问题描述】:

已经在 stackoverflow 上查找了所有问题,但没有任何效果。 如果有人加入特定语音频道,我希望我的机器人发送消息。

client.on('voiceStateUpdate', (oldMember, newMember) => {
  // Here I'm storing the IDs of their voice channels, if available
  const oldChannel = oldMember.voiceChannel ? oldMember.voiceChannel.id : null;
  const newChannel = newMember.voiceChannel ? newMember.voiceChannel.id : null;
  if (oldChannel == newChannel) return; // If there has been no change, exit

  // Here I'm getting the client's channel (client.voiceChannel does not exist)
  const clientMember = oldMember.guild.member(client.user),
    clientChannel = clientMember ? clientMember.voiceChannel.id : null;

  // Here I'm getting the channel, just replace VVV this VVV with the channel's ID
  const textChannel = oldMember.guild.channels.get('765520462439645191');
  if (!textChannel) throw new Error("That channel does not exist.");

  // Here I don't need to check if they're the same, since it would've exit before
  if (newChannel == clientChannel) {
    // console.log("A user joined.");
    textChannel.send(`${newMember} has joined the voice channel.`);
  } else if (oldChannel == clientChannel) {
    // console.log("A user left.");
    textChannel.send(`${newMember} has left the voice channel.`);
  }

不起作用。没有错误,但它不起作用。请帮忙

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    我认为这应该可行:

    client.on('voiceStateUpdate', (oldMember, newMember) => {
      const newUserChannel = newMember.voice.channelID
      const oldUserChannel = oldMember.voice.channelID
      const textChannel = message.guild.channels.cache.get('TEXT CHANNEL ID')
    
      if(newUserChannel === 'VOICE CHANNEL ID') {
        textChannel.send(`${newMember.user.username} (${newMember.id}) has joined the channel`)
      } else if (oldUserChannel === 'VOICE CHANNEL ID' && newUserChannel !== 'VOICE CHANNEL ID') {
        textChannel.send(`${newMember.user.username} (${newMember.id}) has left the channel`)
      }
    })
    

    【讨论】:

    • 虽然这可能会解决 OP 的问题,但添加一些解释为什么您的代码可以工作以及为什么 OP 代码不能真正提高这个答案的价值。
    猜你喜欢
    • 1970-01-01
    • 2020-01-28
    • 2019-01-12
    • 2021-08-27
    • 1970-01-01
    • 2021-09-30
    • 2021-04-05
    • 2020-07-02
    • 2021-08-21
    相关资源
    最近更新 更多