From 25356be323f9e566bf5087db0fec560dc5bc09a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Sun, 5 Sep 2021 02:35:36 +0300 Subject: [PATCH] Moved play function to funcs folder --- src/Client.ts | 1 + src/client/commands/play.js | 93 +------------------------------------ src/client/funcs/play.js | 88 +++++++++++++++++++++++++++++++++++ src/client/restoreradio.js | 87 +--------------------------------- 4 files changed, 93 insertions(+), 176 deletions(-) create mode 100644 src/client/funcs/play.js diff --git a/src/Client.ts b/src/Client.ts index 2d364e5..7c0e3ef 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -40,6 +40,7 @@ class RadioClient extends Client { this.funcs.saveState = require("./client/funcs/saveState.js"); this.funcs.loadState = require("./client/funcs/loadState.js"); this.funcs.searchStation = require("./client/funcs/searchStation.js"); + this.funcs.play = require("./client/funcs/play.js"); console.log('RadioX ' + this.config.version); console.log('Internet Radio to your Discord guild'); diff --git a/src/client/commands/play.js b/src/client/commands/play.js index f15ab44..b4fece3 100644 --- a/src/client/commands/play.js +++ b/src/client/commands/play.js @@ -167,7 +167,7 @@ module.exports = { construct.connection = connection; let date = new Date(); construct.startTime = date.getTime(); - play(interaction, interaction.guild, client, url, Discord); + client.funcs.play(interaction, interaction.guild, client, url, Discord); client.datastore.checkEntry(interaction.guild.id); construct.currentGuild = client.datastore.getEntry(interaction.guild.id); @@ -184,93 +184,4 @@ module.exports = { return interaction.reply(client.messageEmojis["error"] + `An error occured: ${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); - return interaction.reply(client.messages.errorPlaying); - }); - - 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((radio.station.logo || "https://cdn.discordapp.com/emojis/" + client.messageEmojis["play"].replace(/[^0-9]+/g, ''))) - .setColor(client.config.embedColor) - .addField(client.messages.nowplayingTitle, message.nowplayingDescription, true) - .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') - .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') - ) - .addComponents( - new Discord.MessageButton() - .setCustomId('stop') - .setEmoji(client.messageEmojis["stop"]) - .setStyle('SECONDARY') - ) - .addComponents( - new Discord.MessageButton() - .setCustomId('next') - .setEmoji(client.messageEmojis["next"]) - .setStyle('SECONDARY') - ) - .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); - - interaction.reply({ - content: client.messageEmojis["play"] + message.play, - ephemeral: true - }); - -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/client/funcs/play.js b/src/client/funcs/play.js new file mode 100644 index 0000000..0d9551a --- /dev/null +++ b/src/client/funcs/play.js @@ -0,0 +1,88 @@ +module.exports = 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); + return interaction.reply(client.messages.errorPlaying); + }); + + 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((radio.station.logo || "https://cdn.discordapp.com/emojis/" + client.messageEmojis["play"].replace(/[^0-9]+/g, ''))) + .setColor(client.config.embedColor) + .addField(client.messages.nowplayingTitle, message.nowplayingDescription, true) + .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') + .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') + ) + .addComponents( + new Discord.MessageButton() + .setCustomId('stop') + .setEmoji(client.messageEmojis["stop"]) + .setStyle('SECONDARY') + ) + .addComponents( + new Discord.MessageButton() + .setCustomId('next') + .setEmoji(client.messageEmojis["next"]) + .setStyle('SECONDARY') + ) + .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); + + interaction.reply({ + content: client.messageEmojis["play"] + message.play, + ephemeral: true + }); + +} \ No newline at end of file diff --git a/src/client/restoreradio.js b/src/client/restoreradio.js index 1e31709..982bc87 100644 --- a/src/client/restoreradio.js +++ b/src/client/restoreradio.js @@ -43,7 +43,7 @@ module.exports = { let date = new Date(); construct.startTime = date.getTime(); - play(null, guild, client, url, Discord); + client.funcs.play(null, guild, client, url, Discord); client.datastore.checkEntry(guild.id); construct.datastore = client.datastore.getEntry(guild.id); @@ -59,87 +59,4 @@ module.exports = { } }); } -} - -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((radio.station.logo || "https://cdn.discordapp.com/emojis/" + client.messageEmojis["play"].replace(/[^0-9]+/g, ''))) - .setColor(client.config.embedColor) - .addField(client.messages.nowplayingTitle, message.nowplayingDescription, true) - .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') - .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') - ) - .addComponents( - new Discord.MessageButton() - .setCustomId('stop') - .setEmoji(client.messageEmojis["stop"]) - .setStyle('SECONDARY') - ) - .addComponents( - new Discord.MessageButton() - .setCustomId('next') - .setEmoji(client.messageEmojis["next"]) - .setStyle('SECONDARY') - ) - .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); - -} +} \ No newline at end of file