Files
NekoMelody/example/discord.ts

92 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-06-21 12:17:32 +07:00
import NekoMelody, { Player } from "../src";
2024-06-19 11:55:55 +07:00
2024-07-04 21:19:07 +07:00
import {
NoSubscriberBehavior,
VoiceConnectionStatus,
createAudioPlayer,
createAudioResource,
joinVoiceChannel,
} from "@discordjs/voice";
import { YtDlpProvider } from "../src/providers";
2024-07-04 21:19:07 +07:00
import { AudioInformation } from "../src/providers/base";
import { Client, IntentsBitField } from "discord.js";
import dotenv from "dotenv";
dotenv.config();
2024-06-19 11:55:55 +07:00
const main = async () => {
2024-07-04 21:19:07 +07:00
// Discord Client
const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildVoiceStates,
],
});
client.login(process.env.BOT_TOKEN);
// Wait until the client is ready
await new Promise((resolve) => {
client.once("ready", resolve);
});
// Get channel
const guild = client.guilds.cache.get(process.env.GUILD_ID ?? "");
if (!guild) throw new Error("Guild not found");
const member = guild.members.cache.get(process.env.MEMBER_ID ?? "");
if (!member) throw new Error("Member not found");
if (!member.voice.channel) {
throw new Error("Member is not connected to a voice channel.");
}
console.log("Joining voice channel", member.voice.channel.name);
// Join voice channel
const connection = joinVoiceChannel({
channelId: member.voice.channel.id,
guildId: guild.id,
adapterCreator: member.voice.channel.guild.voiceAdapterCreator,
});
// Wait until the connection is ready
await new Promise((resolve) => {
connection.on(VoiceConnectionStatus.Ready, resolve);
});
2024-06-21 12:17:32 +07:00
const videoId = "2gigEGxnsmo";
2024-07-04 21:19:07 +07:00
const videoId2 = "oM-JneFEdBk";
// Providers
const providers = [new YtDlpProvider()];
const player = NekoMelody.createPlayer(providers);
2024-07-04 21:19:07 +07:00
const discordPlayer = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Pause,
},
});
connection.subscribe(discordPlayer);
2024-07-04 21:19:07 +07:00
player.on("play", (information: AudioInformation) => {
if (!player.stream) throw new Error("No input stream");
//playSpeaker(player);
const resource = createAudioResource(player.stream, {
//inlineVolume: true,
});
discordPlayer.play(resource);
2024-07-05 10:50:37 +07:00
player.startCurrentStream();
2024-07-04 21:19:07 +07:00
discordPlayer.on("stateChange", (oldState, newState) => {
console.log("State change", oldState.status, newState.status);
if (oldState.status === "playing" && newState.status === "idle") {
player.endCurrentStream();
}
});
});
2024-07-04 21:19:07 +07:00
await player.enqueue(`https://www.youtube.com/watch?v=${videoId}`);
await player.enqueue(`https://www.youtube.com/watch?v=${videoId2}`);
2024-06-21 12:17:32 +07:00
};
2024-06-19 11:55:55 +07:00
main();