【问题标题】:when bot not in channel, bot will auto join to specific channel. discord js当机器人不在频道中时,机器人将自动加入特定频道。不和谐的js
【发布时间】:2021-01-20 23:44:10
【问题描述】:

我为我的音乐机器人制作了这段代码,但是我知道当机器人不在语音频道中时机器人如何检查,机器人会自动连接回特定的频道,如 id。但是当机器人在另一个语音通道中使用时,机器人不会回到我制作的特定语音通道。了解如何用语音编码间隔、循环或检查机器人。

client.on("ready", () => {
  console.log(`Hello ${client.user.username} is now online!`);

  let channel = client.channels.cache.get("720137811587629119");
  if (!channel) return console.error("The channel does not exist!");
  setInterval(function() {

  channel.join()
  .then(connection => console.log('Connected'))
  .catch(console.error);
}, 30000)

  let botStatus = [
    `${client.guilds.cache.size} servers!`,
    "s,help or s,h",
    `Over ${client.users.cache.size} users!`,
    `Over ${client.channels.cache.size} channels!`
  ]
  
  setInterval(function() {
    let status = botStatus[Math.floor(Math.random() * botStatus.length)];
    client.user.setActivity(status, {type: "PLAYING"});
  
    }, 5000)
  });

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    您可以使用客户端上的voiceStateUpdate 事件来检测您的机器人何时与语音通道断开连接。这是一个例子:

    client.on("voiceStateUpdate", (oldState, newState) => {
      if (newState.id !== client.user.id) return; // do nothing if it's not your bot
      if (oldState.channel.id === "720137811587629119" && !newState.channel) { // check if the bot was disconnected from 720137811587629119
        newState.setChannel("720137811587629119");
      }
    }
    

    【讨论】:

    • 我把代码放在哪里了?替换 channel.join() ?当机器人不使用时,我需要机器人始终加入该频道
    猜你喜欢
    • 2019-06-24
    • 2021-08-14
    • 1970-01-01
    • 2021-10-12
    • 2020-10-15
    • 2021-07-13
    • 2019-12-08
    • 1970-01-01
    • 2021-05-23
    相关资源
    最近更新 更多