Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fetch = require("node-fetch");
- const { SlashCommandBuilder } = require("discord.js");
- const {
- StreamType,
- joinVoiceChannel,
- createAudioPlayer,
- NoSubscriberBehavior,
- createAudioResource,
- AudioPlayerStatus,
- VoiceConnectionStatus,
- } = require("@discordjs/voice");
- module.exports = {
- data: new SlashCommandBuilder()
- .setName("join-channel")
- .setDescription("Makes the bot join the channel"),
- async execute(interaction, client) {
- const message = await interaction.deferReply({
- fetchReply: false,
- });
- const connection = joinVoiceChannel({
- channelId: interaction.member.voice.channel.id,
- guildId: interaction.guildId,
- adapterCreator: interaction.guild.voiceAdapterCreator,
- });
- const resource = createAudioResource(
- "https://simulatorfm.stream/radio/8025/radio.mp3",
- { inlineVolum: true, type: StreamType.Arbitrary }
- );
- const player = createAudioPlayer({
- behaviors: {
- noSubscriber: NoSubscriberBehavior.Play,
- },
- });
- connection.subscribe(player);
- connection.on(VoiceConnectionStatus.Ready, () => {
- player.play(resource);
- })
- connection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
- try {
- console.log("Disconnected.")
- await Promise.race([
- entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
- entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
- ]);
- } catch (error) {
- connection.destroy();
- }
- });
- player.on("error", (error) => {
- console.error(
- `Error: ${error.message} with resource ${error.resource.metadata.title}`
- );
- player.play(getNextResource());
- });
- player.on(AudioPlayerStatus.Idle, () => {
- player.play(getNextResource());
- });
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement