From 5e53a908e78ae1331c6525e54a824e8e95aa85d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 09:38:15 +0300 Subject: [PATCH 01/33] Removed prefix from code --- .env_example | 3 +-- README.md | 5 +---- src/client/commands/help.js | 1 - src/client/messages.js | 2 +- src/config.js | 1 - 5 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.env_example b/.env_example index f312147..e41c215 100644 --- a/.env_example +++ b/.env_example @@ -1,3 +1,2 @@ DISCORD_TOKEN= -RADIOX_STATIONSLISTURL=https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json -RADIOX_PREFIX=rx- \ No newline at end of file +RADIOX_STATIONSLISTURL=https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json \ No newline at end of file diff --git a/README.md b/README.md index aa87e67..53033a4 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,9 @@ Internet Radio to your Discord guild ## [Radio Stations List](https://gitea.cwinfo.org/cwchristerw/radio) This bot is using Gitea repo to get radio stations from [playlist.json](https://gitea.cwinfo.org/cwchristerw/radio/raw/branch/master/playlist.json) file. List is currently maintained by Christer Warén. You can use alternative list with same format when using RADIOX_STATIONSLISTURL environment variable. -## PREFIX -Default prefix is "rx-" and you can change it with using RADIOX_PREFIX environment variable. - ## Docker 1. `docker build -t warengroup/eximiabots-radiox .` -2. `docker run --name radiox-dev -d --net host -e DISCORD_TOKEN= -e RADIOX_PREFIX="rx-" -v "$PWD/datastore":/usr/src/app/datastore/ warengroup/eximiabots-radiox` +2. `docker run --name radiox-dev -d --net host -e DISCORD_TOKEN= -v "$PWD/datastore":/usr/src/app/datastore/ warengroup/eximiabots-radiox` ## Join our Discord Server https://discord.gg/rRA65Mn diff --git a/src/client/commands/help.js b/src/client/commands/help.js index 476def6..fc5b94c 100644 --- a/src/client/commands/help.js +++ b/src/client/commands/help.js @@ -19,7 +19,6 @@ module.exports = { message.helpTitle = client.messages.helpTitle.replace("%client.user.username%", client.user.username); message.helpDescription = client.messages.helpDescription.replace("%commands%", commands); - message.helpDescription = message.helpDescription.replace("%client.config.prefix%", client.config.prefix); const embed = new Discord.MessageEmbed() .setTitle(message.helpTitle) diff --git a/src/client/messages.js b/src/client/messages.js index db7350c..9e44a11 100644 --- a/src/client/messages.js +++ b/src/client/messages.js @@ -8,7 +8,7 @@ module.exports = { bugDescription: "Join the support server \n %client.config.supportGuild%", helpTitle: "%client.user.username% help:", helpDescription: "%commands%", - helpCommandTitle: "%client.config.prefix%%command.name% %command.usage%", + helpCommandTitle: "%command.name% %command.usage%", helpCommandDescription: "%command.description% \n Command Alias: %command.alias%", inviteTitle: "Invite %client.user.username% to your Discord server!", listTitle: "Radio Stations", diff --git a/src/config.js b/src/config.js index 41089fe..2aa884d 100644 --- a/src/config.js +++ b/src/config.js @@ -20,7 +20,6 @@ module.exports = { hostedBy: "[Warén Group](https://waren.io)", //Settings - prefix: process.env.RADIOX_PREFIX || "rx-", version: process.env.RADIOX_VERSION || process.env.npm_package_version, debug: process.env.DEBUG_MODE || false From f786abebe484d569c55d499ab2c3afea933e86e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 12:07:08 +0300 Subject: [PATCH 02/33] Restoring capability to use messageCreate event. --- src/Client.ts | 8 +++++++ src/client/events/messageCreate.js | 37 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/client/events/messageCreate.js diff --git a/src/Client.ts b/src/Client.ts index 18b97d8..9bb882b 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -11,6 +11,7 @@ const GatewayIntents = new Discord.Intents(); GatewayIntents.add( 1 << 0, // GUILDS 1 << 7, // GUILD_VOICE_STATES + 1 << 9 // GUILD_MESSAGES ); class RadioClient extends Client { @@ -46,12 +47,19 @@ class RadioClient extends Client { require(`${events}ready`).execute(this); this.datastore = new Datastore(); }); + + this.on("messageCreate", msg => { + require(`${events}messageCreate`).execute(this, msg); + }); + this.on("interactionCreate", interaction => { require(`${events}interactionCreate`).execute(this, interaction); }); + this.on("voiceStateUpdate", (oldState, newState) => { require(`${events}voiceStateUpdate`).execute(this, oldState, newState); }); + this.on("error", error => { console.error(error); }); diff --git a/src/client/events/messageCreate.js b/src/client/events/messageCreate.js new file mode 100644 index 0000000..782042c --- /dev/null +++ b/src/client/events/messageCreate.js @@ -0,0 +1,37 @@ +import Discord from "discord.js"; +module.exports = { + name: 'messageCreate', + async execute(client, msg) { + + if (msg.author.bot || !msg.guild) return; + let prefix = "rx$"; + if(client.user.username == "RadioX"){ + prefix = "rx>"; + } else if (client.user.username == "RadioX Beta"){ + prefix = "rx-"; + } else if (client.user.username == "RadioX Dev"){ + prefix = "rx$"; + } else if(msg.mentions.members.first() && msg.mentions.members.first().user.id === client.user.id){ + prefix = "<@!" + client.user.id + "> "; + } else { + return; + } + + const args = msg.content.slice(prefix.length).split(' '); + if (!msg.content.startsWith(prefix)) return; + if (!args[0]) return; + const commandName = args[0].toLowerCase(); + if (commandName === 'none') return; + const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName)) || client.commandAliases.get(commandName); + if (!command && msg.content !== `${prefix}`) return; + const permissions = msg.channel.permissionsFor(msg.client.user); + if (!permissions.has('EMBED_LINKS')) return msg.channel.send(client.messages.noPermsEmbed); + try { + + + } catch (error) { + msg.reply(client.messages.runningCommandFailed); + console.error(error); + } + } +} \ No newline at end of file From 8a10697edd32bd9dc078ebce80c8fe914a24ff93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 12:08:19 +0300 Subject: [PATCH 03/33] Adding deprecation message when using message commands. --- src/client/events/messageCreate.js | 10 ++++++++++ src/client/messages.js | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/client/events/messageCreate.js b/src/client/events/messageCreate.js index 782042c..d69322a 100644 --- a/src/client/events/messageCreate.js +++ b/src/client/events/messageCreate.js @@ -27,8 +27,18 @@ module.exports = { const permissions = msg.channel.permissionsFor(msg.client.user); if (!permissions.has('EMBED_LINKS')) return msg.channel.send(client.messages.noPermsEmbed); try { + let message = {}; + message.messageCommandsDeprecatedTitle = client.messages.messageCommandsDeprecatedTitle.replace("%client.user.username%", client.user.username); + + const embed = new Discord.MessageEmbed() + .setTitle(message.messageCommandsDeprecatedTitle) + .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["logo"].replace(/[^0-9]+/g, '')) + .setColor(client.config.embedColor) + .setDescription(client.messages.messageCommandsDeprecatedDescription) + .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); + msg.channel.send({ embeds: [embed] }); } catch (error) { msg.reply(client.messages.runningCommandFailed); console.error(error); diff --git a/src/client/messages.js b/src/client/messages.js index 9e44a11..0ce2a55 100644 --- a/src/client/messages.js +++ b/src/client/messages.js @@ -36,5 +36,7 @@ module.exports = { statusField3: "Uptime", statusField4: "Version", statusField5: "Hosted by", - errorStationURL: "Station can't be URL" + errorStationURL: "Station can't be URL", + messageCommandsDeprecatedTitle: "%client.user.username%", + messageCommandsDeprecatedDescription: "We recommend you to reauthorize our bot with clicking invite link because Discord is planning to remove message content from verified bots [Read More](https://support-dev.discord.com/hc/en-us/articles/4404772028055) \n\n **Invite Bot** \n https://wgi.fi/radiox_invite \n\n This bot is now supporting slash commands, you need to start to use them instead. Type / into message box in Discord and select bot you want to use. Remember to be careful because there are a few bugs here and there in Discord." }; \ No newline at end of file From 5208a3feb16acfc3e29564926fdf6f97f3afc819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 12:11:16 +0300 Subject: [PATCH 04/33] Remove deprecation message after 30 seconds. --- src/client/events/messageCreate.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/client/events/messageCreate.js b/src/client/events/messageCreate.js index d69322a..4c63784 100644 --- a/src/client/events/messageCreate.js +++ b/src/client/events/messageCreate.js @@ -39,6 +39,10 @@ module.exports = { .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); msg.channel.send({ embeds: [embed] }); + + setTimeout(function() { + msg.delete(); + }, 30000); } catch (error) { msg.reply(client.messages.runningCommandFailed); console.error(error); From b6c47d7c6f1665e43f2b5d1cda4a38c6523ad927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 12:49:08 +0300 Subject: [PATCH 05/33] Updated messageCommandsDeprecatedDescription in messages.js --- src/client/messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/messages.js b/src/client/messages.js index 0ce2a55..64627e2 100644 --- a/src/client/messages.js +++ b/src/client/messages.js @@ -38,5 +38,5 @@ module.exports = { statusField5: "Hosted by", errorStationURL: "Station can't be URL", messageCommandsDeprecatedTitle: "%client.user.username%", - messageCommandsDeprecatedDescription: "We recommend you to reauthorize our bot with clicking invite link because Discord is planning to remove message content from verified bots [Read More](https://support-dev.discord.com/hc/en-us/articles/4404772028055) \n\n **Invite Bot** \n https://wgi.fi/radiox_invite \n\n This bot is now supporting slash commands, you need to start to use them instead. Type / into message box in Discord and select bot you want to use. Remember to be careful because there are a few bugs here and there in Discord." + messageCommandsDeprecatedDescription: "We recommend you to reauthorize our bot by clicking the invite link down below, because Discord is planning to remove message content from verified bots [Read More](https://support-dev.discord.com/hc/en-us/articles/4404772028055) \n\n **Invite Bot** \n https://wgi.fi/radiox_invite \n\n This bot now supports slash commands, you should start using them instead. Type / into the message box and select the bot you wish to use. Remember to be careful as there are a few bugs here and there on Discord. \n\n We will remove this deprecation message in March of 2022 when RadioX 1.0.0 is released." }; \ No newline at end of file From c4fb1d514785df7e7e753977bca0bbb789839736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 12:54:39 +0300 Subject: [PATCH 06/33] Updated interactionCreate & messageCreate events --- src/client/events/interactionCreate.js | 10 ++++++++-- src/client/events/messageCreate.js | 5 ++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/client/events/interactionCreate.js b/src/client/events/interactionCreate.js index af105ab..4e3e608 100644 --- a/src/client/events/interactionCreate.js +++ b/src/client/events/interactionCreate.js @@ -16,7 +16,10 @@ module.exports = { try { command.execute(interaction, client, Discord, command); } catch (error) { - interaction.reply(client.messages.runningCommandFailed); + interaction.reply({ + content: client.messages.runningCommandFailed, + ephemeral: true + }); console.error(error); } } else if (interaction.isSelectMenu() || interaction.isButton()){ @@ -27,7 +30,10 @@ module.exports = { try { command.execute(interaction, client, Discord, command); } catch (error) { - interaction.reply(client.messages.runningCommandFailed); + interaction.reply({ + content: client.messages.runningCommandFailed, + ephemeral: true + }); console.error(error); } } diff --git a/src/client/events/messageCreate.js b/src/client/events/messageCreate.js index 4c63784..49614d1 100644 --- a/src/client/events/messageCreate.js +++ b/src/client/events/messageCreate.js @@ -44,7 +44,10 @@ module.exports = { msg.delete(); }, 30000); } catch (error) { - msg.reply(client.messages.runningCommandFailed); + msg.reply({ + content: client.messages.runningCommandFailed, + ephemeral: true + }); console.error(error); } } From a1d4e356a6655a9b9db6725dcac7eaa64247cb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 13:12:52 +0300 Subject: [PATCH 07/33] Removed comment in interactionCreate.js --- src/client/events/interactionCreate.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client/events/interactionCreate.js b/src/client/events/interactionCreate.js index 4e3e608..67d2666 100644 --- a/src/client/events/interactionCreate.js +++ b/src/client/events/interactionCreate.js @@ -3,7 +3,6 @@ import Discord from "discord.js"; module.exports = { name: 'interactionCreate', async execute(client, interaction) { - /*if (!interaction.isCommand()) return;*/ const permissions = interaction.channel.permissionsFor(interaction.client.user); if (!permissions.has('EMBED_LINKS')) return interaction.send(client.messages.noPermsEmbed); From 492df398ffd1f79336b27935a87a1b86daf92079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 13:16:42 +0300 Subject: [PATCH 08/33] Removed unnecessary messages in messages.js --- src/client/messages.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/client/messages.js b/src/client/messages.js index 64627e2..46eda2d 100644 --- a/src/client/messages.js +++ b/src/client/messages.js @@ -8,8 +8,6 @@ module.exports = { bugDescription: "Join the support server \n %client.config.supportGuild%", helpTitle: "%client.user.username% help:", helpDescription: "%commands%", - helpCommandTitle: "%command.name% %command.usage%", - helpCommandDescription: "%command.description% \n Command Alias: %command.alias%", inviteTitle: "Invite %client.user.username% to your Discord server!", listTitle: "Radio Stations", nowplayingTitle: "Now Playing", From 1fd6c2c6916862737b572d9ee30cf5d76c67dd14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 14:39:23 +0300 Subject: [PATCH 09/33] Added messageDelete event --- src/Client.ts | 4 ++++ src/client/events/messageDelete.js | 10 ++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/client/events/messageDelete.js diff --git a/src/Client.ts b/src/Client.ts index 9bb882b..1b7e14c 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -52,6 +52,10 @@ class RadioClient extends Client { require(`${events}messageCreate`).execute(this, msg); }); + this.on("messageDelete", msg => { + require(`${events}messageDelete`).execute(this, msg); + }); + this.on("interactionCreate", interaction => { require(`${events}interactionCreate`).execute(this, interaction); }); diff --git a/src/client/events/messageDelete.js b/src/client/events/messageDelete.js new file mode 100644 index 0000000..9cce584 --- /dev/null +++ b/src/client/events/messageDelete.js @@ -0,0 +1,10 @@ +module.exports = { + name: 'messageDelete', + async execute(client, msg) { + if (!msg.author.bot || !msg.guild) return; + const radio = client.radio.get(msg.guild.id); + if (!radio) return; + if(msg.id != radio.message.id) return; + radio.message = null; + } +} \ No newline at end of file From fea24ec1aae2463f9a11648c2bdaa9183438bd4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 14:39:52 +0300 Subject: [PATCH 10/33] Edited message in play.js --- src/client/commands/play.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client/commands/play.js b/src/client/commands/play.js index 2ba4d3e..c110352 100644 --- a/src/client/commands/play.js +++ b/src/client/commands/play.js @@ -214,6 +214,7 @@ async function play(interaction, guild, client, url, Discord) { message.nowplayingDescription = client.messages.nowplayingDescription.replace("%radio.station.name%", radio.station.name); message.nowplayingDescription = message.nowplayingDescription.replace("%radio.station.owner%", radio.station.owner); message.nowplayingDescription = message.nowplayingDescription.replace("%client.funcs.msToTime(completed)%", ""); + message.nowplayingDescription = message.nowplayingDescription.replace("Owner: ", ""); message.nowplayingDescription = message.nowplayingDescription.replace("**", ""); message.nowplayingDescription = message.nowplayingDescription.replace("**", ""); From eba7551329abae945ebc6b35ec44120d999d953e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 14:40:12 +0300 Subject: [PATCH 11/33] Edited loggers in ready.js --- src/client/events/ready.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client/events/ready.js b/src/client/events/ready.js index 2213918..a6a7125 100644 --- a/src/client/events/ready.js +++ b/src/client/events/ready.js @@ -10,7 +10,7 @@ module.exports = { console.log(''); /*DEVELOPERS*/ - client.funcs.logger('Developers', 'List'); + client.funcs.logger('Developers'); client.developers = ""; let user = ""; @@ -32,7 +32,7 @@ module.exports = { .then(client.funcs.checkFetchStatus) .then(response => response.json()); - client.funcs.logger('Stations', 'List'); + client.funcs.logger('Stations'); client.stations.forEach(station => { console.log(" - " + station.name); }); @@ -65,7 +65,7 @@ module.exports = { /*GUILDS*/ client.funcs.logger('Guilds', 'Started fetching list'); - client.funcs.logger('Guilds', 'List'); + client.funcs.logger('Guilds'); let guilds = await client.guilds.fetch(); guilds.forEach(guild => { console.log(" - " + guild.id + ": " + guild.name); From 45bd690c052468343c9bfebba2e0c0f54091e81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 14:40:26 +0300 Subject: [PATCH 12/33] Updated logger function --- src/client/funcs/logger.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client/funcs/logger.js b/src/client/funcs/logger.js index 851a8d1..80ce35c 100644 --- a/src/client/funcs/logger.js +++ b/src/client/funcs/logger.js @@ -1,4 +1,5 @@ module.exports = function (area, text){ let date = new Date(); - console.log('[' + area + '] – ' + date.toISOString() + '\n' + text + '\n'); + console.log('[' + area + '] – ' + date.toISOString()); + if(text) console.log(text + '\n'); } \ No newline at end of file From da71137f68ce4e8244301895e8e69290f1e00c21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 14:40:55 +0300 Subject: [PATCH 13/33] Updated stop.js --- src/client/commands/stop.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client/commands/stop.js b/src/client/commands/stop.js index b2e8641..e0d1779 100644 --- a/src/client/commands/stop.js +++ b/src/client/commands/stop.js @@ -21,13 +21,13 @@ module.exports = { .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); if(!radio.message){ - radio.message = await radio.textChannel.send({ embeds: [embed], components: [] }); + radio.message = radio.textChannel.send({ embeds: [embed], components: [] }); } else { radio.message.edit({ embeds: [embed], components: [] }); } - setTimeout(function() { - radio.message.delete(); + setTimeout(async function() { + await radio.message?.delete(); }, 5000); client.radio.delete(interaction.guild.id); From 12337f499bd6784d075300a6a012b443aeb81314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 15:00:04 +0300 Subject: [PATCH 14/33] Updated list command --- src/client/commands/list.js | 81 +++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/src/client/commands/list.js b/src/client/commands/list.js index 40e62da..fd7b412 100644 --- a/src/client/commands/list.js +++ b/src/client/commands/list.js @@ -11,22 +11,71 @@ module.exports = { message.errorToGetPlaylist = client.messages.errorToGetPlaylist.replace("%client.config.supportGuild%", client.config.supportGuild); return interaction.reply(client.messageEmojis["error"] + message.errorToGetPlaylist); } - let stations = `${client.stations.map(s => `**#** ${s.name}`).join('\n')}` - const hashs = stations.split('**#**').length; - for (let i = 0; i < hashs; i++) { - stations = stations.replace('**#**', `**${i + 1}.**`); - } - - const embed = new Discord.MessageEmbed() - .setTitle(client.messages.listTitle) - .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["list"].replace(/[^0-9]+/g, '')) - .setColor(client.config.embedColor) - .setDescription(stations) - .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); - interaction.reply({ - embeds: [embed], - ephemeral: true - }); + const radio = client.radio.get(interaction.guild.id); + + if(radio){ + let menu = []; + + let stations = new Array(); + + let options = new Array(); + options[1] = new Array(); + options[2] = new Array(); + + stations[1] = client.stations.slice(0,24).forEach(station => { + station = { + label: station.name, + description: station.owner, + value: station.name + }; + options[1].push(station); + }); + + stations[2] = client.stations.slice(25).forEach(station => { + station = { + label: station.name, + description: station.owner, + value: station.name + }; + options[2].push(station); + }); + + menu = new Discord.MessageActionRow() + .addComponents( + new Discord.MessageSelectMenu() + .setCustomId('play') + .setPlaceholder('Change station') + .addOptions(options[1]) + .addOptions(options[2]) + ); + + stations = null; + options = null; + + interaction.reply({ + content: '**Select station:**', + components: [menu], + ephemeral: true + }); + } else { + let stations = `${client.stations.map(s => `**#** ${s.name}`).join('\n')}` + const hashs = stations.split('**#**').length; + for (let i = 0; i < hashs; i++) { + stations = stations.replace('**#**', `**${i + 1}.**`); + } + + let embed = new Discord.MessageEmbed() + .setTitle(client.messages.listTitle) + .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["list"].replace(/[^0-9]+/g, '')) + .setColor(client.config.embedColor) + .setDescription(stations) + .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); + + interaction.reply({ + embeds: [embed], + ephemeral: true + }); + } } }; \ No newline at end of file From 706e65bc069b51a602f166e1e707fc50bc3a6fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 16:50:01 +0300 Subject: [PATCH 15/33] Added SIGINT event --- src/Client.ts | 11 +++++++++ src/client/events/SIGINT.js | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/client/events/SIGINT.js diff --git a/src/Client.ts b/src/Client.ts index 1b7e14c..7e29d4a 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -37,6 +37,13 @@ class RadioClient extends Client { this.funcs.msToTime = require("./client/funcs/msToTime.js"); this.funcs.statisticsUpdate = require("./client/funcs/statisticsUpdate.js"); + console.log('RadioX ' + this.config.version); + console.log('Internet Radio to your Discord guild'); + console.log('(c)2020-2021 EximiaBots by Warén Group'); + console.log(''); + + this.funcs.logger("Bot", "Starting"); + const commandFiles = fs.readdirSync(path.join("./src/client/commands")).filter(f => f.endsWith(".js")); for (const file of commandFiles) { const command = require(`./client/commands/${file}`); @@ -64,6 +71,10 @@ class RadioClient extends Client { require(`${events}voiceStateUpdate`).execute(this, oldState, newState); }); + process.on('SIGINT', () => { + require(`${events}SIGINT`).execute(this); + }); + this.on("error", error => { console.error(error); }); diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js new file mode 100644 index 0000000..3debd5d --- /dev/null +++ b/src/client/events/SIGINT.js @@ -0,0 +1,46 @@ +import Discord from "discord.js"; + +module.exports = { + name: 'SIGINT', + async execute(client) { + /*setTimeout(function () { + let message = {}; + + if (!client.stations) return process.exit(); + + let currentRadios = client.radio.keys(); + let radio = currentRadios.next(); + let stoppedRadios = ""; + client.user.setStatus('idle'); + + while (!radio.done) { + + let currentRadio = client.radio.get(radio.value); + currentRadio.guild = client.datastore.getEntry(radio.value).guild; + + if (currentRadio) { + client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); + currentRadio.connection?.destroy(); + currentRadio.audioPlayer?.stop(); + currentRadio.message?.delete(); + const cembed = new Discord.MessageEmbed() + .setTitle(client.messages.maintenanceTitle) + .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["maintenance"].replace(/[^0-9]+/g, '')) + .setColor(client.config.embedColor) + .setDescription(client.messages.sendedMaintenanceMessage) + .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); + currentRadio.textChannel.send({ embeds: [cembed] }); + client.radio.delete(radio.value); + stoppedRadios += "-" + radio.value + ": " + currentRadio.guild.name + "\n"; + } + radio = currentRadios.next(); + } + + console.log("\n"); + client.funcs.logger("Bot", "Closing"); + console.log("\n");*/ + + process.exit(); + /*}, 5000);*/ + } +} \ No newline at end of file From bf0811945ac857bfdb64fbd89d1ef816cbcd3ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 16:50:16 +0300 Subject: [PATCH 16/33] Fixed statisticsUpdate in maintenance.js --- src/client/commands/maintenance.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/commands/maintenance.js b/src/client/commands/maintenance.js index e4b836f..4da0981 100644 --- a/src/client/commands/maintenance.js +++ b/src/client/commands/maintenance.js @@ -26,7 +26,7 @@ module.exports = { currentRadio.guild = client.datastore.getEntry(radio.value).guild; if(currentRadio){ - client.funcs.statisticsUpdate(client, currentRadio.currentGuild.guild, currentRadio); + client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); currentRadio.connection?.destroy(); currentRadio.audioPlayer?.stop(); const cembed = new Discord.MessageEmbed() @@ -37,7 +37,7 @@ module.exports = { .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); currentRadio.textChannel.send({ embeds: [cembed] }); client.radio.delete(radio.value); - stoppedRadios += "-" + radio.value + ": " + currentRadio.currentGuild.guild.name + "\n"; + stoppedRadios += "-" + radio.value + ": " + currentRadio.guild.name + "\n"; } radio = currentRadios.next(); } From 46237fd91e80b398d28bd708d2d7d5f9eaea9b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 16:50:45 +0300 Subject: [PATCH 17/33] Updated ready.js --- src/client/events/ready.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/client/events/ready.js b/src/client/events/ready.js index a6a7125..1706b52 100644 --- a/src/client/events/ready.js +++ b/src/client/events/ready.js @@ -4,10 +4,7 @@ module.exports = { name: 'ready', async execute(client) { - console.log('RadioX ' + client.config.version); - console.log('Internet Radio to your Discord guild'); - console.log('(c)2020-2021 EximiaBots by Warén Group'); - console.log(''); + client.funcs.logger("Bot", "Ready"); /*DEVELOPERS*/ client.funcs.logger('Developers'); @@ -16,7 +13,7 @@ module.exports = { let user = ""; for (let i = 0; i < client.config.devId.length; i++) { user = await client.users.fetch(client.config.devId[i]); - console.log(" - " + user.tag); + console.log("- " + user.tag); if (i == client.config.devId.length - 1) { client.developers += user.tag; } else { @@ -34,7 +31,7 @@ module.exports = { client.funcs.logger('Stations'); client.stations.forEach(station => { - console.log(" - " + station.name); + console.log("- " + station.name); }); console.log("\n"); @@ -68,7 +65,7 @@ module.exports = { client.funcs.logger('Guilds'); let guilds = await client.guilds.fetch(); guilds.forEach(guild => { - console.log(" - " + guild.id + ": " + guild.name); + console.log("- " + guild.id + ": " + guild.name); }); console.log("\n"); From 74239f71a76da5369a099b1aa11ae90a4f3af4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 17:37:07 +0300 Subject: [PATCH 18/33] Updated SIGINT event --- src/client/events/SIGINT.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js index 3debd5d..ec06265 100644 --- a/src/client/events/SIGINT.js +++ b/src/client/events/SIGINT.js @@ -3,7 +3,8 @@ import Discord from "discord.js"; module.exports = { name: 'SIGINT', async execute(client) { - /*setTimeout(function () { + setTimeout(async function () { + client.user.setStatus('idle'); let message = {}; if (!client.stations) return process.exit(); @@ -14,12 +15,11 @@ module.exports = { client.user.setStatus('idle'); while (!radio.done) { - - let currentRadio = client.radio.get(radio.value); - currentRadio.guild = client.datastore.getEntry(radio.value).guild; + let currentRadio = await client.radio.get(radio.value); + currentRadio.guild = await client.datastore.getEntry(radio.value).guild; if (currentRadio) { - client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); + await client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); currentRadio.connection?.destroy(); currentRadio.audioPlayer?.stop(); currentRadio.message?.delete(); @@ -31,16 +31,22 @@ module.exports = { .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); currentRadio.textChannel.send({ embeds: [cembed] }); client.radio.delete(radio.value); - stoppedRadios += "-" + radio.value + ": " + currentRadio.guild.name + "\n"; } - radio = currentRadios.next(); + + radio = await currentRadios.next(); } + + console.log("\n"); client.funcs.logger("Bot", "Closing"); - console.log("\n");*/ + console.log("\n"); - process.exit(); - /*}, 5000);*/ + client.user.setStatus('dnd'); + + setTimeout(function () { + process.exit(); + }, 5000); + }, 5000); } } \ No newline at end of file From 65f83bc5222c22488139a69e6f6ddd91f8edb110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 17:57:37 +0300 Subject: [PATCH 19/33] Updated SIGINT event --- src/client/events/SIGINT.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js index ec06265..6186db3 100644 --- a/src/client/events/SIGINT.js +++ b/src/client/events/SIGINT.js @@ -11,15 +11,13 @@ module.exports = { let currentRadios = client.radio.keys(); let radio = currentRadios.next(); - let stoppedRadios = ""; - client.user.setStatus('idle'); while (!radio.done) { - let currentRadio = await client.radio.get(radio.value); - currentRadio.guild = await client.datastore.getEntry(radio.value).guild; + let currentRadio = client.radio.get(radio.value); + currentRadio.guild = client.datastore.getEntry(radio.value).guild; if (currentRadio) { - await client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); + client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); currentRadio.connection?.destroy(); currentRadio.audioPlayer?.stop(); currentRadio.message?.delete(); @@ -33,7 +31,7 @@ module.exports = { client.radio.delete(radio.value); } - radio = await currentRadios.next(); + radio = currentRadios.next(); } @@ -44,9 +42,11 @@ module.exports = { client.user.setStatus('dnd'); - setTimeout(function () { - process.exit(); - }, 5000); + setInterval(() => { + if(radio.done){ + process.exit(); + } + }, 1000); }, 5000); } } \ No newline at end of file From 61981a5b0b333e014ab321134b3ca597aebff982 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 17:58:31 +0300 Subject: [PATCH 20/33] Updated SIGINT event --- src/client/events/SIGINT.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js index 6186db3..d8668dc 100644 --- a/src/client/events/SIGINT.js +++ b/src/client/events/SIGINT.js @@ -21,13 +21,6 @@ module.exports = { currentRadio.connection?.destroy(); currentRadio.audioPlayer?.stop(); currentRadio.message?.delete(); - const cembed = new Discord.MessageEmbed() - .setTitle(client.messages.maintenanceTitle) - .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["maintenance"].replace(/[^0-9]+/g, '')) - .setColor(client.config.embedColor) - .setDescription(client.messages.sendedMaintenanceMessage) - .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); - currentRadio.textChannel.send({ embeds: [cembed] }); client.radio.delete(radio.value); } From 8e9546097f1bcb6543565ba0e9e05bac73e0aa74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 18:18:37 +0300 Subject: [PATCH 21/33] Updated Client.ts --- src/Client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Client.ts b/src/Client.ts index 7e29d4a..95a3235 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -1,12 +1,13 @@ import Discord, { Client, Collection } from "discord.js"; import fs from "fs"; -const events = "./client/events/"; import Datastore from "./client/datastore.js"; import { command, radio } from "./client/utils/typings.js"; import config from "./config.js"; import messages from "./client/messages.js"; import path from "path"; +const events = "./client/events/"; + const GatewayIntents = new Discord.Intents(); GatewayIntents.add( 1 << 0, // GUILDS From 66ace838c6d9740142e03282ade7318476dcfa98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 18:45:45 +0300 Subject: [PATCH 22/33] Remove unnecessary lines in SIGINT.js --- src/client/events/SIGINT.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js index d8668dc..95aadf4 100644 --- a/src/client/events/SIGINT.js +++ b/src/client/events/SIGINT.js @@ -27,8 +27,6 @@ module.exports = { radio = currentRadios.next(); } - - console.log("\n"); client.funcs.logger("Bot", "Closing"); console.log("\n"); From 45b23722fa1c8cdf512246329ce5ef429e1ef785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 18:58:23 +0300 Subject: [PATCH 23/33] Updated maintenance command --- src/client/commands/maintenance.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/client/commands/maintenance.js b/src/client/commands/maintenance.js index 4da0981..1574c4c 100644 --- a/src/client/commands/maintenance.js +++ b/src/client/commands/maintenance.js @@ -29,13 +29,7 @@ module.exports = { client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); currentRadio.connection?.destroy(); currentRadio.audioPlayer?.stop(); - const cembed = new Discord.MessageEmbed() - .setTitle(client.messages.maintenanceTitle) - .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["maintenance"].replace(/[^0-9]+/g, '')) - .setColor(client.config.embedColor) - .setDescription(client.messages.sendedMaintenanceMessage) - .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); - currentRadio.textChannel.send({ embeds: [cembed] }); + currentRadio.message?.delete(); client.radio.delete(radio.value); stoppedRadios += "-" + radio.value + ": " + currentRadio.guild.name + "\n"; } From 430e0f9e513e22384cf612c4c5ad346ba4dd4755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 19:18:20 +0300 Subject: [PATCH 24/33] Added SIGTERM event --- src/Client.ts | 4 ++++ src/client/events/SIGTERM.js | 6 ++++++ 2 files changed, 10 insertions(+) create mode 100644 src/client/events/SIGTERM.js diff --git a/src/Client.ts b/src/Client.ts index 95a3235..e5ef66b 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -75,6 +75,10 @@ class RadioClient extends Client { process.on('SIGINT', () => { require(`${events}SIGINT`).execute(this); }); + + process.on('SIGTERM', () => { + require(`${events}SIGTERM`).execute(this); + }); this.on("error", error => { console.error(error); diff --git a/src/client/events/SIGTERM.js b/src/client/events/SIGTERM.js new file mode 100644 index 0000000..52daa75 --- /dev/null +++ b/src/client/events/SIGTERM.js @@ -0,0 +1,6 @@ +module.exports = { + name: 'SIGTERM', + async execute(client) { + process.emit('SIGINT'); + } +} \ No newline at end of file From a1f85a0d19896a3d60383b9c2b563238cc5abb49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Thu, 2 Sep 2021 19:25:04 +0300 Subject: [PATCH 25/33] Updated SIGINT.js --- src/client/events/SIGINT.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js index 95aadf4..d3826a0 100644 --- a/src/client/events/SIGINT.js +++ b/src/client/events/SIGINT.js @@ -4,7 +4,6 @@ module.exports = { name: 'SIGINT', async execute(client) { setTimeout(async function () { - client.user.setStatus('idle'); let message = {}; if (!client.stations) return process.exit(); From f2158ba8ae59982ca9389c6824179865201963cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:21:11 +0300 Subject: [PATCH 26/33] Updated datastore.js --- src/client/datastore.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/client/datastore.js b/src/client/datastore.js index dafd80a..dbacf73 100644 --- a/src/client/datastore.js +++ b/src/client/datastore.js @@ -80,6 +80,7 @@ module.exports = class { newData.guild = {}; newData.guild.id = id; newData.statistics = {}; + newData.state = {}; this.map.set(id, newData); this.saveEntry(id, newData); } @@ -90,6 +91,10 @@ module.exports = class { updateEntry(guild, newData) { newData.guild.name = guild.name; + + let date = new Date(); + newData.updated = date.toISOString().substring(0, 10) + this.map.set(guild.id, newData); this.saveEntry(guild.id, newData); //this.showEntry(this.getEntry(guild.id)); @@ -110,6 +115,9 @@ module.exports = class { "time": 0, "used": 0 } + }, + "state": { + } } @@ -118,7 +126,7 @@ module.exports = class { saveEntry(file, data) { data = JSON.stringify(data, null, 4); - + fs.writeFile(path.join(path.dirname(__dirname), '../datastore') + "/" + file + ".json", data, 'utf8', function(err) { if (err) { //console.log(err); From e546001be8c1e95f0e3a765ffb7e504bf61110fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:21:55 +0300 Subject: [PATCH 27/33] Added saveState & loadState functions --- src/Client.ts | 3 ++- src/client/funcs/loadState.js | 10 ++++++++++ src/client/funcs/saveState.js | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/client/funcs/loadState.js create mode 100644 src/client/funcs/saveState.js diff --git a/src/Client.ts b/src/Client.ts index e5ef66b..a991ab7 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -37,6 +37,8 @@ class RadioClient extends Client { this.funcs.logger = require("./client/funcs/logger.js"); this.funcs.msToTime = require("./client/funcs/msToTime.js"); this.funcs.statisticsUpdate = require("./client/funcs/statisticsUpdate.js"); + this.funcs.saveState = require("./client/funcs/saveState.js"); + this.funcs.loadState = require("./client/funcs/loadState.js"); console.log('RadioX ' + this.config.version); console.log('Internet Radio to your Discord guild'); @@ -53,7 +55,6 @@ class RadioClient extends Client { this.on("ready", () => { require(`${events}ready`).execute(this); - this.datastore = new Datastore(); }); this.on("messageCreate", msg => { diff --git a/src/client/funcs/loadState.js b/src/client/funcs/loadState.js new file mode 100644 index 0000000..f7ff54b --- /dev/null +++ b/src/client/funcs/loadState.js @@ -0,0 +1,10 @@ +module.exports = function loadState(client, guild){ + let data = client.datastore.getEntry(guild.id); + if(!data) return; + let state; + + state = data.state; + data.state = {}; + client.datastore.updateEntry(guild, data); + return state; +} \ No newline at end of file diff --git a/src/client/funcs/saveState.js b/src/client/funcs/saveState.js new file mode 100644 index 0000000..eb59553 --- /dev/null +++ b/src/client/funcs/saveState.js @@ -0,0 +1,18 @@ +module.exports = function saveState(client, guild, radio){ + client.datastore.checkEntry(guild.id); + + let date = new Date(); + + let data = client.datastore.getEntry(guild.id); + + data.state = {}; + data.state.channels = {}; + data.state.channels.text = radio.textChannel.id; + data.state.channels.voice = radio.voiceChannel.id; + data.state.date = date.toISOString(); + data.state.station = {}; + data.state.station.name = radio.station.name; + data.state.station.owner = radio.station.owner; + + client.datastore.updateEntry(guild, data); +} \ No newline at end of file From 5ec4ea6aaeb9aad2f63c643d0cb31c3a43078ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:22:25 +0300 Subject: [PATCH 28/33] Updated SIGINT.js --- src/client/events/SIGINT.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client/events/SIGINT.js b/src/client/events/SIGINT.js index d3826a0..dc0bf65 100644 --- a/src/client/events/SIGINT.js +++ b/src/client/events/SIGINT.js @@ -17,6 +17,7 @@ module.exports = { if (currentRadio) { client.funcs.statisticsUpdate(client, currentRadio.guild, currentRadio); + client.funcs.saveState(client, currentRadio.guild, currentRadio); currentRadio.connection?.destroy(); currentRadio.audioPlayer?.stop(); currentRadio.message?.delete(); From 329986be53cbd77cd1a9008096f69e2fc0e88e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:22:47 +0300 Subject: [PATCH 29/33] Added restoreradio.js --- src/client/restoreradio.js | 207 +++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 src/client/restoreradio.js diff --git a/src/client/restoreradio.js b/src/client/restoreradio.js new file mode 100644 index 0000000..74fdaac --- /dev/null +++ b/src/client/restoreradio.js @@ -0,0 +1,207 @@ +import Discord from "discord.js"; +const { + createAudioPlayer, + createAudioResource, + getVoiceConnection, + joinVoiceChannel +} = require("@discordjs/voice"); + +module.exports = { + async execute(client, guilds) { + guilds.forEach(async guild => { + let state = client.funcs.loadState(client, guild); + if(!state) return; + if(!state.station || !state.channels.voice || !state.channels.text) return; + + const sstation = await searchStation(state.station.name, client); + let url = sstation.stream[sstation.stream.default]; + let station = sstation; + + const construct = { + textChannel: client.channels.cache.get(state.channels.text), + voiceChannel: client.channels.cache.get(state.channels.voice), + connection: null, + message: null, + audioPlayer: createAudioPlayer(), + station: station + }; + client.radio.set(guild.id, construct); + + try { + let voiceChannel = client.channels.cache.get(state.channels.voice); + const connection = + getVoiceConnection(guild.id) ?? + joinVoiceChannel({ + channelId: voiceChannel.id, + guildId: voiceChannel.guild.id, + adapterCreator: voiceChannel.guild.voiceAdapterCreator + }); + + construct.connection = connection; + let date = new Date(); + construct.startTime = date.getTime(); + + play(null, guild, client, url, Discord); + + client.datastore.checkEntry(guild.id); + construct.datastore = client.datastore.getEntry(guild.id); + + if (!construct.datastore.statistics[construct.station.name]) { + construct.datastore.statistics[construct.station.name] = {}; + construct.datastore.statistics[construct.station.name].time = 0; + construct.datastore.statistics[construct.station.name].used = 0; + client.datastore.updateEntry(guild, construct.datastore); + } + } catch (error) { + console.log(error); + } + }); + } +} + +async function play(interaction, guild, client, url, Discord) { + let message = {}; + const radio = client.radio.get(guild.id); + const resource = createAudioResource(url); + radio.connection.subscribe(radio.audioPlayer); + radio.audioPlayer.play(resource); + resource.playStream + .on("readable", () => { + client.funcs.logger('Radio', 'Stream started' + " / " + guild.id + " / " + radio.station.name); + }) + .on("finish", () => { + client.funcs.logger('Radio', 'Stream finished' + " / " + guild.id); + client.funcs.statisticsUpdate(client, guild, radio); + radio.connection?.destroy(); + radio.audioPlayer?.stop(); + client.radio.delete(guild.id); + return; + }) + .on("error", error => { + client.funcs.logger('Radio', 'Stream errored'); + console.error(error); + radio.connection?.destroy(); + radio.audioPlayer?.stop(); + client.radio.delete(guild.id); + }); + + message.nowplayingDescription = client.messages.nowplayingDescription.replace("%radio.station.name%", radio.station.name); + message.nowplayingDescription = message.nowplayingDescription.replace("%radio.station.owner%", radio.station.owner); + message.nowplayingDescription = message.nowplayingDescription.replace("%client.funcs.msToTime(completed)%", ""); + message.nowplayingDescription = message.nowplayingDescription.replace("Owner: ", ""); + message.nowplayingDescription = message.nowplayingDescription.replace("**", ""); + message.nowplayingDescription = message.nowplayingDescription.replace("**", ""); + + const embed = new Discord.MessageEmbed() + .setTitle(client.user.username) + .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["play"].replace(/[^0-9]+/g, '')) + .setColor(client.config.embedColor) + .addField(client.messages.nowplayingTitle, message.nowplayingDescription, true) + .setFooter(client.messages.footerText, "https://cdn.discordapp.com/emojis/" + client.messageEmojis["eximiabots"].replace(/[^0-9]+/g, '')); + + const buttons = new Discord.MessageActionRow() + .addComponents( + new Discord.MessageButton() + .setCustomId('list') + .setEmoji(client.messageEmojis["list"]) + .setStyle('SECONDARY') + ) + .addComponents( + new Discord.MessageButton() + .setCustomId('prev') + .setEmoji(client.messageEmojis["prev"]) + .setStyle('SECONDARY') + .setDisabled(true) + ) + .addComponents( + new Discord.MessageButton() + .setCustomId('stop') + .setEmoji(client.messageEmojis["stop"]) + .setStyle('SECONDARY') + ) + .addComponents( + new Discord.MessageButton() + .setCustomId('next') + .setEmoji(client.messageEmojis["next"]) + .setStyle('SECONDARY') + .setDisabled(true) + ) + .addComponents( + new Discord.MessageButton() + .setCustomId('statistics') + .setEmoji(client.messageEmojis["statistics"]) + .setStyle('SECONDARY') + ); + + if(!radio.message){ + radio.message = await radio.textChannel.send({ embeds: [embed], components: [buttons] }); + } else { + radio.message.edit({ embeds: [embed], components: [buttons] }); + } + + message.play = client.messages.play.replace("%radio.station.name%", radio.station.name); + +} + +function searchStation(key, client) { + if (client.stations === null) return false; + let foundStations = []; + if (!key) return false; + if (key == "radio") return false; + + client.stations + .filter( + x => x.name.toUpperCase().includes(key.toUpperCase()) || x === key + ) + .forEach(x => + foundStations.push({ station: x, name: x.name, probability: 100 }) + ); + + if (key.startsWith("radio ")) key = key.slice(6); + 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; + 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 From a8fc38ffca33d33d13b3ca819623e70fbcd90608 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:23:17 +0300 Subject: [PATCH 30/33] Updated ready.js --- src/client/events/ready.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/client/events/ready.js b/src/client/events/ready.js index 1706b52..e89d3b0 100644 --- a/src/client/events/ready.js +++ b/src/client/events/ready.js @@ -1,4 +1,5 @@ -const fetch = require('node-fetch'); +import Datastore from "../datastore.js"; +import fetch from "node-fetch"; module.exports = { name: 'ready', @@ -6,6 +7,10 @@ module.exports = { client.funcs.logger("Bot", "Ready"); + /*Datastore*/ + client.funcs.logger('Datastore', 'Initialize'); + client.datastore = new Datastore(); + /*DEVELOPERS*/ client.funcs.logger('Developers'); @@ -70,7 +75,7 @@ module.exports = { console.log("\n"); client.funcs.logger('Guilds', 'Successfully fetched list'); - + /*STATISTICS*/ client.datastore.calculateGlobal(client); @@ -80,5 +85,10 @@ module.exports = { /*COMMANDS*/ require(`../commands.js`).execute(client); + setTimeout(function () { + /*RESTORE RADIO*/ + require(`../restoreradio.js`).execute(client, guilds); + }, 5000); + } } \ No newline at end of file From 179914a2f28a8794d470c1f18c72d6ed00e8891e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:23:35 +0300 Subject: [PATCH 31/33] Updated statisticsUpdate.js --- src/client/funcs/statisticsUpdate.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/client/funcs/statisticsUpdate.js b/src/client/funcs/statisticsUpdate.js index 2ca66c6..4fcf09c 100644 --- a/src/client/funcs/statisticsUpdate.js +++ b/src/client/funcs/statisticsUpdate.js @@ -2,21 +2,21 @@ module.exports = function statisticsUpdate(client, guild, radio) { client.datastore.checkEntry(guild.id); - radio.currentGuild = client.datastore.getEntry(guild.id); + radio.datastore = client.datastore.getEntry(guild.id); - if(!radio.currentGuild.statistics[radio.station.name]){ - radio.currentGuild.statistics[radio.station.name] = {}; - radio.currentGuild.statistics[radio.station.name].time = 0; - radio.currentGuild.statistics[radio.station.name].used = 0; - client.datastore.updateEntry(guild, radio.currentGuild); + if(!radio.datastore.statistics[radio.station.name]){ + radio.datastore.statistics[radio.station.name] = {}; + radio.datastore.statistics[radio.station.name].time = 0; + radio.datastore.statistics[radio.station.name].used = 0; + client.datastore.updateEntry(guild, radio.datastore); } let date = new Date(); radio.currentTime = date.getTime(); radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime); - radio.currentGuild.statistics[radio.station.name].time = parseInt(radio.currentGuild.statistics[radio.station.name].time)+parseInt(radio.playTime); + radio.datastore.statistics[radio.station.name].time = parseInt(radio.datastore.statistics[radio.station.name].time)+parseInt(radio.playTime); - radio.currentGuild.statistics[radio.station.name].used = parseInt(radio.currentGuild.statistics[radio.station.name].used)+1; - client.datastore.updateEntry(guild, radio.currentGuild); + radio.datastore.statistics[radio.station.name].used = parseInt(radio.datastore.statistics[radio.station.name].used)+1; + client.datastore.updateEntry(guild, radio.datastore); client.datastore.calculateGlobal(client); } \ No newline at end of file From 7d4fb503513c90384f20ed35e5a7f5bcb47d8144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:24:21 +0300 Subject: [PATCH 32/33] Update startTime when changing stations in play.js --- src/client/commands/play.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/commands/play.js b/src/client/commands/play.js index c110352..c0b6d01 100644 --- a/src/client/commands/play.js +++ b/src/client/commands/play.js @@ -137,8 +137,10 @@ module.exports = { client.funcs.statisticsUpdate(client, interaction.guild, radio); radio.audioPlayer.stop(); + let date = new Date(); radio.station = station; radio.textChannel = interaction.channel; + radio.startTime = date.getTime(); play(interaction, interaction.guild, client, url, Discord); return; From 3ec1892a0935aa75d142cd15f1b2760ca2fe49a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 3 Sep 2021 01:26:00 +0300 Subject: [PATCH 33/33] Updated ready.js --- src/client/events/ready.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/events/ready.js b/src/client/events/ready.js index e89d3b0..b9498e5 100644 --- a/src/client/events/ready.js +++ b/src/client/events/ready.js @@ -7,7 +7,7 @@ module.exports = { client.funcs.logger("Bot", "Ready"); - /*Datastore*/ + /*DATASTORE*/ client.funcs.logger('Datastore', 'Initialize'); client.datastore = new Datastore();