Discord.js在文本通道中加入/离开语音通道通知

时间:2020-05-20 16:39:08

标签: bots discord discord.js

我正在尝试创建一个机器人,当有人加入特定的语音通道(例如VC1)时,它将向特定的文本通道(例如vc-text)发送消息。

这是bot.js代码:

const Discord = require('discord.js');
const {token} = require('./auth.json');
const bot = new Discord.Client();

bot.login(token);

bot.once('ready', () =>{
    console.log(`Bot ready, logged in as ${bot.user.tag}!`);
})

bot.on('voiceStateUpdate', (oldMember, newMember) => {
    let newUserChannel = newMember.voiceChannelID
    let oldUserChannel = oldMember.voiceChannelID

    if(newUserChannel === 712677767333937284) {
        // User Joins a voice channel
        console.log("Joined VC1")

    } else if(newUserChannel !== 712677767333937284){
        // User leaves a voice channel
        console.log("Left VC1")

    }
})

某些ID:

  • 文本通道ID为712677731023716452-向(I发送通知 知道我需要添加频道消息编码)。
  • VC1 ID为 712677767333937284-触发加入

当我加入VC1时,会收到控制台消息“左VC1”,而当我离开/加入另一个VC1时,也会收到相同的控制台消息。

我从https://www.reddit.com/r/discordapp/comments/6p85uf/discordjs_any_way_to_detect_when_someone_enter_a/那里获得了机器人示例

2 个答案:

答案 0 :(得分:0)

首先,由于您使用的是discord.js v12,member.voiceChannelID不再有用,您需要使用member.voice.channelID

您可以使用其ID或名称获取textChannel:

通过ID

const textChannel = message.guild.channels.cache.get('712677731023716452')

按名称

const textChannel = message.guild.channels.cache.find(channel => channel.name === 'vc-text')

解决方案:

bot.on('voiceStateUpdate', (oldMember, newMember) => {
  const newUserChannel = newMember.voice.channelID
  const oldUserChannel = oldMember.voice.channelID
  const textChannel = message.guild.channels.cache.get('712677731023716452')

  if(newUserChannel === '712677767333937284') {
    textChannel.send(`${newMember.user.username} (${newMember.id}) has joined the channel`)
  } else if (oldUserChannel === '712677767333937284' && newUserChannel !== '712677767333937284') {
    textChannel.send(`${newMember.user.username} (${newMember.id}) has left the channel`)
  }
})

答案 1 :(得分:0)

这应该有效

    bot.on('voiceStateUpdate', (oldMember, newMember) => {
       let newUserChannel = newMember.channelID;
       let oldUserChannel = oldMember.channelID;
    
       if(newUserChannel === "Channel id here") //don't remove ""
       { 
           // User Joins a voice channel
           console.log("Joined vc with id "+newUserChannel);
       }
       else{
           // User leaves a voice channel
           console.log("Left vc");
       }
    });