克隆了一个GitHub project
并想稍微修改一下。bot
基本上Youtube livestream
在voice channel
24/7中播放。我想让它bot
在没有人时停止播放,voice channel
并在成员加入时立即开始voice channel
。由于对 的知识为零discord.js
,我发现很难实现我的目标。
以下是code
使用的:
const { Client } = require("discord.js");
const keepAlive = require('./server.js');
const client = new Client({
disableEveryone: true
});
keepAlive();
client.login(process.env.TOKEN);
const { TOKEN, CHANNEL, STATUS, LIVE } = require("./config.json");
const discord = require("discord.js");
const ytdl = require('ytdl-core');
let broadcast = null;
let interval = null;
if (!TOKEN) {
console.error("Press provide a valid Discord Bot Token.");
return process.exit(1);
} else if (!CHANNEL || Number(CHANNEL) == NaN) {
console.log("Please provide a valid channel ID.");
return process.exit(1);
} else if (!LIVE) {
console.log("Please provide a valid Youtube URL.");
return process.exit(1);
}
client.on('ready', async () => {
client.user.setActivity(STATUS, { type: "STREAMING", url:"https://www.youtube.com/watch?v=Igipt9XegCs"});
let channel = client.channels.cache.get(CHANNEL) || await client.channels.fetch(CHANNEL)
if (!channel) {
console.error("The provided channel ID doesn't exist, or I don't have permission to view that channel. Because of that, I'm aborting now.");
return process.exit(1);
} else if (channel.type !== "voice") {
console.error("The provided channel ID is NOT voice channel. Because of that, I'm aborting now.");
return process.exit(1);
}
broadcast = client.voice.createBroadcast();
// Play the radio
stream = await ytdl(LIVE);
stream.on('error', console.error);
broadcast.play(stream);
// Make interval so radio will automatically reconnect to YT every 30 minute because YT will change the raw url every 30m/1 Hour
const GuildMember = new Discord.GuildMember(); // This shall be the command author.
if (!interval) {
interval = setInterval(async function() {
try {
stream = await ytdl(LIVE, { highWaterMark: 100 << 150 });
stream.on('error', console.error);
broadcast.play(stream);
} catch (e) { return }
}, 1800000)
}
try {
const connection = await channel.join();
connection.play(broadcast);
} catch (error) {
console.error(error);
}
});
setInterval(async function() {
if(!client.voice.connections.size) {
let channel = client.channels.cache.get(CHANNEL) || await client.channels.fetch(CHANNEL);
if(!channel) return;
try {
const connection = await channel.join();
connection.play(broadcast);
} catch (error) {
console.error(error);
}
}
}, 20000);
client.login(TOKEN) //Login
process.on('unhandledRejection', console.error);
我已经尝试过使用voiceStateUpdate
,但我认为我犯了很多错误:
client.on('voiceStateUpdate', (oldState, newState) => {
if (oldState.channelID !== oldState.guild.me.voice.channelID || newState.channel)
return;
if (!oldState.channel.members.size - 1)
setTimeout(() => {
if (!oldState.channel.members.size - 1) // 1 member,
broadcast.exit();
}, 5000);
});
任何帮助表示赞赏。