From c21082c67ba8b0d7001a937339ec6f11e1f66eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Tue, 28 Nov 2023 13:28:17 +0200 Subject: [PATCH] Add duration to Play command --- src/client/funcs/play.ts | 101 +++++++++++++++++++++++---------------- src/client/messages.ts | 4 +- 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/client/funcs/play.ts b/src/client/funcs/play.ts index 283a7aa..fc773db 100644 --- a/src/client/funcs/play.ts +++ b/src/client/funcs/play.ts @@ -10,13 +10,13 @@ export default async function play(client: RadioClient, interaction: ChatInputCo radio.connection.subscribe(audioPlayer); client.funcs.logger('Radio', guild.id + " / " + "Play" + " / " + radio.station.name); - if(radio.station?.playlist?.type == "supla" || radio.station?.playlist?.type == "yle"){ - let playlist: any = await fetch(radio.station.playlist.address) + try { + if(radio.station?.playlist?.type == "supla" || radio.station?.playlist?.type == "yle"){ + let playlist: any = await fetch(radio.station.playlist.address) .then((response: Response) => response.json()) .catch(error => { }); - try { switch(radio.station?.playlist.type){ case "supla": radio.station.track = "__" + playlist.items[0]?.artist + "__" + "\n" + playlist.items[0]?.song; @@ -27,9 +27,9 @@ export default async function play(client: RadioClient, interaction: ChatInputCo default: radio.station.track = "-"; } - } catch(TypeError) { - } + } catch(TypeError) { + } const embed = new EmbedBuilder() @@ -48,7 +48,14 @@ export default async function play(client: RadioClient, interaction: ChatInputCo value: client.messages.replace(client.messages.playDescription2, { "%radio.station.track%": radio.station.track != undefined ? "\n\n" + radio.station.track : "-" }) + }, + { + name: client.messages.playTitle3, + value: client.messages.replace(client.messages.playDescription3, { + "%client.funcs.msToTime(completed)%": "-" + }) }) + .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') .setFooter({ text: client.messages.footerText, @@ -99,18 +106,19 @@ export default async function play(client: RadioClient, interaction: ChatInputCo } setInterval(async function(){ + const radio = client.radio?.get(guild.id); let changed = false; - if(radio.station?.playlist?.type == "supla" || radio.station?.playlist?.type == "yle"){ - let playlist: any = await fetch(radio.station.playlist.address) + try { + if(radio.station?.playlist?.type == "supla" || radio.station?.playlist?.type == "yle"){ + let playlist: any = await fetch(radio.station.playlist.address) .then((response: Response) => response.json()) .catch(error => { }); - try { + switch(radio.station?.playlist.type){ case "supla": - if(radio.station.track != playlist.items[0].artist + "\n" + playlist.items[0].song){ - changed = true; + if(radio.station.track != "__" + playlist.items[0]?.artist + "__" + "\n" + playlist.items[0].song){ radio.station.track = "__" + playlist.items[0].artist + "__" + "\n" + playlist.items[0].song; } break; @@ -120,45 +128,54 @@ export default async function play(client: RadioClient, interaction: ChatInputCo default: radio.station.track = "-"; } - } catch(TypeError) { - } + } catch(TypeError) { + } - if(changed == true){ - const embed = new EmbedBuilder() - .setTitle(client.user?.username || "-") - .setThumbnail((radio.station.logo || "https://cdn.discordapp.com/emojis/" + client.messages.emojis["play"].replace(/[^0-9]+/g, ''))) - .setColor(client.config.embedColor) - .addFields({ - name: client.messages.playTitle1, - value: client.messages.replace(client.messages.playDescription1, { - "%radio.station.name%": radio.station.name, - "%radio.station.owner%": radio.station.name != radio.station.owner ? radio.station.owner + "\n" : "" - }) - }, - { - name: client.messages.playTitle2, - value: client.messages.replace(client.messages.playDescription2, { - "%radio.station.track%": radio.station.track != undefined ? "\n\n" + radio.station.track : "-" - }) + let date = new Date(); + radio.currentTime = date.getTime(); + radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime); + const completed = (radio.playTime); + + const embed = new EmbedBuilder() + .setTitle(client.user?.username || "-") + .setThumbnail((radio.station.logo || "https://cdn.discordapp.com/emojis/" + client.messages.emojis["play"].replace(/[^0-9]+/g, ''))) + .setColor(client.config.embedColor) + .addFields({ + name: client.messages.playTitle1, + value: client.messages.replace(client.messages.playDescription1, { + "%radio.station.name%": radio.station.name, + "%radio.station.owner%": radio.station.name != radio.station.owner ? radio.station.owner + "\n" : "" }) - .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') - .setFooter({ - text: client.messages.footerText, - iconURL: "https://cdn.discordapp.com/emojis/" + client.messages.emojis["eximiabots"].replace(/[^0-9]+/g, '') - }); + }, + { + name: client.messages.playTitle2, + value: client.messages.replace(client.messages.playDescription2, { + "%radio.station.track%": radio.station.track != undefined ? "\n\n" + radio.station.track : "-" + }) + }, + { + name: client.messages.playTitle3, + value: client.messages.replace(client.messages.playDescription3, { + "%client.funcs.msToTime(completed)%": client.funcs.msToTime(completed) + }) + }) + .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') + .setFooter({ + text: client.messages.footerText, + iconURL: "https://cdn.discordapp.com/emojis/" + client.messages.emojis["eximiabots"].replace(/[^0-9]+/g, '') + }); - if(!radio.message){ - radio.message = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }); + if(!radio.message){ + radio.message = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }); + } else { + if(radio.textChannel.id == radio.message.channel.id){ + radio.message.edit({ embeds: [embed], components: [buttons] }); } else { - if(radio.textChannel.id == radio.message.channel.id){ - radio.message.edit({ embeds: [embed], components: [buttons] }); - } else { - radio.message?.delete(); - radio.message = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }); - } + radio.message?.delete(); + radio.message = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }); } } },15000); diff --git a/src/client/messages.ts b/src/client/messages.ts index 4dd4d1a..440db1d 100644 --- a/src/client/messages.ts +++ b/src/client/messages.ts @@ -26,8 +26,8 @@ export const messages = { playDescription1: "__%radio.station.name%__" + "\n" + "%radio.station.owner%", playTitle2: ":musical_note: Track", playDescription2: "%radio.station.track%", - nowplayingTitle: "Now Playing", - nowplayingDescription: "**%radio.station.name%**" + "\n" + "%radio.station.owner%" + "\n" + "%client.funcs.msToTime(completed)%" + "\n\n" + "%radio.station.track%", + playTitle3: ":stopwatch: Duration", + playDescription3: "%client.funcs.msToTime(completed)%", noVoiceChannel: "You need to be in a voice channel to play radio!", noQuery: "You need to use a number or search for a supported station!", noPermsConnect: "I cannot connect to your voice channel.",