Advertisement
DubStepMad

Untitled

Jul 2nd, 2023
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. const fetch = require("node-fetch");
  2. const { SlashCommandBuilder } = require("discord.js");
  3. const {
  4. StreamType,
  5. joinVoiceChannel,
  6. createAudioPlayer,
  7. NoSubscriberBehavior,
  8. createAudioResource,
  9. AudioPlayerStatus,
  10. VoiceConnectionStatus,
  11. } = require("@discordjs/voice");
  12.  
  13. module.exports = {
  14. data: new SlashCommandBuilder()
  15. .setName("join-channel")
  16. .setDescription("Makes the bot join the channel"),
  17. async execute(interaction, client) {
  18. const message = await interaction.deferReply({
  19. fetchReply: false,
  20. });
  21.  
  22. const connection = joinVoiceChannel({
  23. channelId: interaction.member.voice.channel.id,
  24. guildId: interaction.guildId,
  25. adapterCreator: interaction.guild.voiceAdapterCreator,
  26. });
  27.  
  28. const resource = createAudioResource(
  29. "https://simulatorfm.stream/radio/8025/radio.mp3",
  30. { inlineVolum: true, type: StreamType.Arbitrary }
  31. );
  32.  
  33. const player = createAudioPlayer({
  34. behaviors: {
  35. noSubscriber: NoSubscriberBehavior.Play,
  36. },
  37. });
  38.  
  39. connection.subscribe(player);
  40. connection.on(VoiceConnectionStatus.Ready, () => {
  41. player.play(resource);
  42. })
  43. connection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => {
  44. try {
  45. console.log("Disconnected.")
  46. await Promise.race([
  47. entersState(connection, VoiceConnectionStatus.Signalling, 5_000),
  48. entersState(connection, VoiceConnectionStatus.Connecting, 5_000),
  49. ]);
  50. } catch (error) {
  51. connection.destroy();
  52. }
  53. });
  54. player.on("error", (error) => {
  55. console.error(
  56. `Error: ${error.message} with resource ${error.resource.metadata.title}`
  57. );
  58. player.play(getNextResource());
  59. });
  60.  
  61. player.on(AudioPlayerStatus.Idle, () => {
  62. player.play(getNextResource());
  63. });
  64. },
  65. };
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement