From bdf2025558fa61af2be7171783bb2a8bf9fff21a Mon Sep 17 00:00:00 2001 From: MatteZ02 <47610069+MatteZ02@users.noreply.github.com> Date: Mon, 9 Mar 2020 13:17:47 +0200 Subject: [PATCH] Search Stations and large update --- commands/bug.js | 14 +------------- commands/nowplaying.js | 5 ++--- commands/play.js | 35 +++++++++++++++++++++++++--------- events/ready.js | 18 ++++++++++++++---- events/voiceStateUpdate.js | 1 - struct/funcs/play.js | 20 +------------------ struct/funcs/searchStation.js | 36 +++++++++++++++++++++++++++++++++++ 7 files changed, 80 insertions(+), 49 deletions(-) create mode 100644 struct/funcs/searchStation.js diff --git a/commands/bug.js b/commands/bug.js index cd0a0a7..44132e1 100644 --- a/commands/bug.js +++ b/commands/bug.js @@ -6,22 +6,10 @@ module.exports = { permission: 'none', category: 'info', async execute(msg, args, client, Discord, prefix) { - - let developers = ""; - let user = ""; - for(i = 0; i < client.config.devId.length; i++){ - user = await client.users.fetch(client.config.devId[i]); - if(i == client.config.devId.length-1){ - developers += user.tag; - } else { - developers += user.tag + " & "; - } - } - const embed = new Discord.MessageEmbed() .setTitle(`Found a bug with ${client.user.username}?\nDM the core developer:`) .setColor(client.config.embedColor) - .setDescription(`${developers}\nOr join the support server: ${client.config.supportGuild}`) + .setDescription(`${client.developers}\nOr join the support server: ${client.config.supportGuild}`) .setFooter('EximiaBots by Warén Media'); msg.channel.send(embed); }, diff --git a/commands/nowplaying.js b/commands/nowplaying.js index 0e1d6f7..4a3a45a 100644 --- a/commands/nowplaying.js +++ b/commands/nowplaying.js @@ -7,15 +7,14 @@ module.exports = { category: 'music', async execute(msg, args, client, Discord, prefix) { const radio = client.radio.get(msg.guild.id); - if (!radio) return msg.channel.send('<:redx:674263474704220182> There is nothing playing.'); - if (!radio.playing) return msg.channel.send('<:redx:674263474704220182> There is nothing playing.'); + if (!radio || !radio.playing) return msg.channel.send('<:redx:674263474704220182> There is nothing playing.'); radio.time = radio.connection.dispatcher.streamTime; const completed = (radio.time.toFixed(0)); const embed = new Discord.MessageEmbed() .setTitle(" Now Playing") .setColor(client.config.embedColor) - .setDescription(`**${client.stations[radio.station].name}** \n Owner: ${client.stations[radio.station].owner} \n\`${client.funcs.msToTime(completed, "hh:mm:ss")}\``) + .setDescription(`**${radio.station.name}** \n Owner: ${radio.station.owner} \n\`${client.funcs.msToTime(completed, "hh:mm:ss")}\``) .setFooter('EximiaBots by Warén Media'); return msg.channel.send(embed); } diff --git a/commands/play.js b/commands/play.js index 6ab6874..f41e99c 100644 --- a/commands/play.js +++ b/commands/play.js @@ -6,7 +6,7 @@ module.exports = { permission: 'none', category: 'music', async execute(msg, args, client, Discord, prefix) { - const station = args[1] ? args[1].replace(/<(.+)>/g, "$1") : ""; + let url = args[1] ? args[1].replace(/<(.+)>/g, "$1") : ""; const radio = client.radio.get(msg.guild.id); const voiceChannel = msg.member.voice.channel; if (!radio) { @@ -22,29 +22,46 @@ module.exports = { if (!permissions.has('SPEAK')) { return msg.channel.send('<:redx:674263474704220182> I cannot speak in your voice channel, make sure I have the proper permissions!'); } + let station; + const number = parseInt(args[1] - 1); + if (url.startsWith('http')) { + return; + } else if (!isNaN(number)) { + if (number > client.stations.length - 1) { + return radio.textChannel.send('<:redx:674263474704220182> no such station!'); + } else { + url = client.stations[number].stream[client.stations[number].stream.default]; + station = client.stations[number]; + } + } else { + const sstation = await client.funcs.searchStation(args.slice(1).join(' '), client); + if (sstation === false) return msg.channel.send('No stations found!'); + url = sstation.stream[sstation.stream.default]; + station = sstation + } if (radio) { radio.connection.dispatcher.destroy(); - radio.station = station; - client.funcs.play(msg.guild, client, station); + radio.station = station; + client.funcs.play(msg.guild, client, url); return; } - - const construct = { + + const construct = { textChannel: msg.channel, voiceChannel: voiceChannel, connection: null, playing: false, - station: station-1, - name: null, + station: station, volume: client.config.volume, + time: null }; client.radio.set(msg.guild.id, construct); - + try { const connection = await voiceChannel.join(); construct.connection = connection; - client.funcs.play(msg.guild, client, station); + client.funcs.play(msg.guild, client, url); } catch (error) { client.radio.delete(msg.guild.id); client.debug_channel.send("Error with connecting to voice channel: " + error); diff --git a/events/ready.js b/events/ready.js index fd0652b..a6e2f31 100644 --- a/events/ready.js +++ b/events/ready.js @@ -3,17 +3,27 @@ const fetch = require('node-fetch'); module.exports = { name: 'ready', async execute(client, Discord) { - + console.log('RadioX'); console.log('We will bring you finnish radio to your discord server'); console.log('(c)2020 EximiaBots by Warén Media / Christer Warén & MatteZ02'); - + + let user = ""; + for (i = 0; i < client.config.devId.length; i++) { + user = await client.users.fetch(client.config.devId[i]); + if (i == client.config.devId.length - 1) { + client.developers += user.tag; + } else { + client.developers += user.tag + " & "; + } + } + client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') .then(res => res.json()); - + setInterval(async () => { client.stations = await fetch('https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json') .then(res => res.json()); - }, 3600); + }, 3600000); } } \ No newline at end of file diff --git a/events/voiceStateUpdate.js b/events/voiceStateUpdate.js index 70b96ff..e55f845 100644 --- a/events/voiceStateUpdate.js +++ b/events/voiceStateUpdate.js @@ -8,7 +8,6 @@ module.exports = { if (newState.member.voice.channel === null) { radio.songs = []; radio.looping = false; - radio.endReason = "manual disconnect"; return client.radio.delete(newState.guild.id); } if (newState.member.voice.channel !== radio.voiceChannel) { diff --git a/struct/funcs/play.js b/struct/funcs/play.js index 3348b43..c6b74fb 100644 --- a/struct/funcs/play.js +++ b/struct/funcs/play.js @@ -1,24 +1,6 @@ -module.exports = async function (guild, client, station) { +module.exports = async function (guild, client, url) { const radio = client.radio.get(guild.id); - let url = ""; - - if (isNaN(station)) { - radio.voiceChannel.leave(); - return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!'); - } - - if (station - 1 > client.stations.length - 1) { - radio.voiceChannel.leave(); - return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!'); - } - - url = client.stations[station - 1].stream[client.stations[station - 1].stream.default]; - - if (!url) { - radio.voiceChannel.leave(); - return radio.textChannel.send('<:redx:674263474704220182> An error has occured while playing radio!'); - } const dispatcher = radio.connection .play(url, { bitrate: 1024, passes: 10, volume: 1, highWaterMark: 1 << 25 }) diff --git a/struct/funcs/searchStation.js b/struct/funcs/searchStation.js new file mode 100644 index 0000000..05b16b3 --- /dev/null +++ b/struct/funcs/searchStation.js @@ -0,0 +1,36 @@ +module.exports = function (key, client) { + let foundStations = []; + if (!key) return false; + const probabilityIncrement = 100 / key.split(' ').length / 2; + for (let i = 0; i < key.split(' ').length; i++) { + client.stations.filter(x => x.name.toUpperCase().includes(key.split(' ')[i].toUpperCase()) || x === key).forEach(x => foundStations.push({ station: x, name: x.name, probability: probabilityIncrement })); + } + if (foundStations.length === 0) return false; + for (let i = 0; i < foundStations.length; i++) { + for (let j = 0; j < foundStations.length; j++) { + if (foundStations[i] === foundStations[j] && i !== j) foundStations.splice(i, 1); + } + } + for (let i = 0; i < foundStations.length; i++) { + if (foundStations[i].name.length > key.length) { + foundStations[i].probability -= (foundStations[i].name.split(' ').length - key.split(' ').length) * (probabilityIncrement * 0.5); + } else if (foundStations[i].name.length === key.length) { + foundStations[i].probability += (probabilityIncrement * 0.9); + } + + for (let j = 0; j < key.split(' ').length; j++) { + if (!foundStations[i].name.toUpperCase().includes(key.toUpperCase().split(' ')[j])) { + foundStations[i].probability -= (probabilityIncrement * 0.5); + } + } + } + let highestProbabilityStation; + console.log('Stations found: ', foundStations); + for (let i = 0; i < foundStations.length; i++) { + if (!highestProbabilityStation || highestProbabilityStation.probability < foundStations[i].probability) highestProbabilityStation = foundStations[i]; + if (highestProbabilityStation && highestProbabilityStation.probability === foundStations[i].probability) { + highestProbabilityStation = foundStations[i].station; + } + } + return highestProbabilityStation; +}; \ No newline at end of file