首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当bot不在通道中时,bot将自动加入特定的通道。不一致的js

当bot不在通道中时,bot将自动加入特定的通道。不一致的js
EN

Stack Overflow用户
提问于 2020-10-06 13:40:31
回答 1查看 244关注 0票数 0

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

代码语言:javascript
复制
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)
  });
EN

回答 1

Stack Overflow用户

发布于 2020-10-06 18:34:51

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

代码语言:javascript
复制
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");
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64219969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档