我为我的音乐机器人做了这段代码,但当机器人不在语音通道时,机器人如何检查,机器人会自动连接回特定的通道,如id。但是,当机器人在另一个语音通道中使用时,机器人不会返回到我制作的特定语音通道。idk如何编码间隔,循环,或检查机器人是否在语音中。
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)
});发布于 2020-10-06 18:34:51
您可以在客户端使用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");
}
}https://stackoverflow.com/questions/64219969
复制相似问题