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 ++++++++++++++++++++++++++++++++++++++++ src/configs/commands.config.json | 5 ++++ src/configs/radio.json | 32 ++++++++++++++++++++ 4 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 src/commands/tune.js create mode 100644 src/configs/radio.json (limited to 'src') 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 diff --git a/src/configs/commands.config.json b/src/configs/commands.config.json index cdb7745..9ef5a75 100644 --- a/src/configs/commands.config.json +++ b/src/configs/commands.config.json @@ -9,6 +9,11 @@ "name": "radioStop", "description": "Stop Radio", "file": "./commands/radioStop.js" + }, + { + "name": "tune", + "description": "FM Radio Tune IN", + "file": "./commands/tune.js" } ] } \ No newline at end of file diff --git a/src/configs/radio.json b/src/configs/radio.json new file mode 100644 index 0000000..2626cdc --- /dev/null +++ b/src/configs/radio.json @@ -0,0 +1,32 @@ +[ + { + "id": "ananthapuriFM", + "name": "Ananthapuri FM", + "url": "https://air.pc.cdn.bitgravity.com/air/live/pbaudio229/playlist.m3u8" + }, + { + "id": "clubFM", + "name": "Club FM 99.6", + "url": "https://22243.live.streamtheworld.com/CLUBFMUAEAAC.aac" + }, + { + "id": "nigthRide", + "name": "Nightride FM", + "url": "https://stream.nightride.fm/nightride.m4a" + }, + { + "id": "recordSynthwave", + "name": "Record Synthwave", + "url": "http://air.radiorecord.ru:805/synth_320" + }, + { + "id": "radioMango", + "name": "Radio Mango 91.9", + "url": "https://bcovlive-a.akamaihd.net/19b535b7499a4719a5c19e043063f5d9/ap-southeast-1/6034685947001/playlist.m3u8" + }, + { + "id": "oneFM", + "name": "One FM 91.3 Singapore", + "url": "https://22253.live.streamtheworld.com/ONE_FM_913AAC.aac?dist=sphradio-web" + } +] \ No newline at end of file -- cgit v1.2.3