From 07d768cc40b3c6a8ea68e0ce1eae17f025f5ba0f Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Tue, 22 Jun 2021 21:52:15 +0530 Subject: Adds FM Radio Stations ``` !tune => This will play default radio station !tune next => Next Radio Station !tune prev => Previous Radio Station ``` --- src/commands/radioStart.js | 11 ++++---- src/commands/tune.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/commands/tune.js (limited to 'src/commands') diff --git a/src/commands/radioStart.js b/src/commands/radioStart.js index d9c4ac1..0f0b5f7 100644 --- a/src/commands/radioStart.js +++ b/src/commands/radioStart.js @@ -1,6 +1,5 @@ const { VoiceChannel, TextChannel, MessageEmbed } = require("discord.js"); const axios = require('axios').default; - const instance = axios.create({ baseURL: 'http://retrowave.ru/api/v1' }); @@ -10,15 +9,15 @@ const resourceUrl = 'http://retrowave.ru' async function broadcastRadio(client, args, message, voiceChannel, textChannel) { const connection = await voiceChannel.join(); try { - playRadio(connection, textChannel); + playRadioRetroWave(connection, textChannel); } catch (error) { console.log(error); textChannel.send('Something went wrong. RADIO MON got ill...😱😱'); } - } -async function playRadio(connection,textChannel) { + +async function playRadioRetroWave(connection, textChannel) { const nextMusic = await getNextMusic(); const { id, title, streamUrl, artworkUrl } = nextMusic; const nowPlayingMessage = new MessageEmbed() @@ -26,10 +25,10 @@ async function playRadio(connection,textChannel) { .setDescription(title) .setColor('LUMINOUS_VIVID_PINK') .setThumbnail(`${resourceUrl}${artworkUrl}`); - textChannel.send(nowPlayingMessage) + textChannel.send(nowPlayingMessage); connection.play(`${resourceUrl}${streamUrl}`, { seek: 0, volume: 1 }) .on('finish', () => { - playRadio(connection,textChannel); + playRadio(connection, textChannel); }); } diff --git a/src/commands/tune.js b/src/commands/tune.js new file mode 100644 index 0000000..e00b50d --- /dev/null +++ b/src/commands/tune.js @@ -0,0 +1,64 @@ +const { VoiceChannel, TextChannel, MessageEmbed } = require('discord.js'); +const fs = require('fs'); +const readFile = fs.promises.readFile; +let radioIndex = 0; + +async function broadcastRadio(client, args, message, voiceChannel, textChannel) { + const connection = await voiceChannel.join(); + try { + playRadio(connection, textChannel, args); + } catch (error) { + console.log(error); + textChannel.send('Something went wrong. RADIO MON got ill...😱😱'); + } +} + +async function playRadio(connection, textChannel, args) { + try { + const [radioCommand] = args; + const radioConfig = await readFile('src/configs/radio.json', 'utf8'); + const radioStations = JSON.parse(radioConfig); + const totalRadioStations = radioStations.length; + let currentRadioStation; + if (!radioCommand) { + radioIndex = 0; + currentRadioStation = radioStations[radioIndex]; + } else { + switch (radioCommand) { + case 'next': + radioIndex++; + if (radioIndex >= totalRadioStations) radioIndex = 0; + break; + case 'prev': + radioIndex--; + if (radioIndex < 0) radioIndex = totalRadioStations - 1; + currentRadioStation = radioStations[radioIndex]; + break; + } + } + currentRadioStation = radioStations[radioIndex]; + connection.play(`${currentRadioStation.url}`, { seek: 0, volume: 1 }); + const nowPlayingMessage = new MessageEmbed() + .setTitle("Now Playing") + .setDescription(currentRadioStation.name) + .setColor('LUMINOUS_VIVID_PINK'); + textChannel.send(nowPlayingMessage) + } catch (error) { + console.log(error); + }; +}; + +module.exports = { + async execute(client, message, args) { + const { RADIO_CHANNEL, NOW_PLAYING_CHANNEL } = process.env; + if (!RADIO_CHANNEL) return message.reply(`Please add RADIO_CHANNEL to .env with a Voice Channel ID`); + if (!NOW_PLAYING_CHANNEL) return message.reply(`Please add NOW_PLAYING_CHANNEL to .env with a Text Channel ID`); + + const voiceChannel = await client.channels.fetch(process.env.RADIO_CHANNEL); + const nowPlayingChannel = await client.channels.fetch(process.env.NOW_PLAYING_CHANNEL); + if (!(voiceChannel instanceof VoiceChannel)) return message.reply(`Please provice a Voice Channel ID to the RADIO_CHANNEL`); + if (!(nowPlayingChannel instanceof TextChannel)) return message.reply(`Please provice a Text Channel ID to the NOW_PLAYING_CHANNEL`); + + broadcastRadio(client, args, message, voiceChannel, nowPlayingChannel); + } +} \ No newline at end of file -- cgit v1.2.3