Moved play function to funcs folder

This commit is contained in:
Christer Warén 2021-09-05 02:35:36 +03:00
parent e8d90f8a3c
commit 25356be323
4 changed files with 93 additions and 176 deletions

View File

@ -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');

View File

@ -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);
@ -185,92 +185,3 @@ 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
});
}

88
src/client/funcs/play.js Normal file
View File

@ -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
});
}

View File

@ -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);
@ -60,86 +60,3 @@ 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);
}