diff --git a/package-lock.json b/package-lock.json index d6edca5..63d4b50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,29 +1,29 @@ { "name": "eximiabots-radiox", - "version": "0.5.2", + "version": "0.5.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "eximiabots-radiox", - "version": "0.5.2", + "version": "0.5.3", "license": "MIT", "dependencies": { "@discordjs/builders": "^1.7.0", "@discordjs/opus": "^0.9.0", "@discordjs/rest": "^2.2.0", "@discordjs/voice": "^0.16.1", - "discord-api-types": "^0.37.63", + "discord-api-types": "^0.37.65", "discord.js": "^14.14.1", "dotenv": "^16.3.1", "libsodium-wrappers": "^0.7.13", "path": "^0.12.7" }, "devDependencies": { - "@types/node": "^20.9.4", + "@types/node": "^20.10.0", "@types/ws": "^8.5.9", - "@typescript-eslint/eslint-plugin": "^6.10.0", - "@typescript-eslint/parser": "^6.11.0", + "@typescript-eslint/eslint-plugin": "^6.13.1", + "@typescript-eslint/parser": "^6.13.1", "eslint": "^8.54.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.1", @@ -562,9 +562,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.9.4", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.9.4.tgz", - "integrity": "sha512-wmyg8HUhcn6ACjsn8oKYjkN/zUzQeNtMy44weTJSM6p4MMzEOuKbA3OjJ267uPCOW7Xex9dyrNTful8XTQYoDA==", + "version": "20.10.0", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.0.tgz", + "integrity": "sha512-D0WfRmU9TQ8I9PFx9Yc+EBHw+vSpIub4IDvQivcp26PtPrdMGAq5SDcpXEo/epqa/DXotVpekHiLNTg3iaKXBQ==", "dependencies": { "undici-types": "~5.26.4" } @@ -584,16 +584,16 @@ } }, "node_modules/@typescript-eslint/eslint-plugin": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.12.0.tgz", - "integrity": "sha512-XOpZ3IyJUIV1b15M7HVOpgQxPPF7lGXgsfcEIu3yDxFPaf/xZKt7s9QO/pbk7vpWQyVulpJbu4E5LwpZiQo4kA==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.13.1.tgz", + "integrity": "sha512-5bQDGkXaxD46bPvQt08BUz9YSaO4S0fB1LB5JHQuXTfkGPI3+UUeS387C/e9jRie5GqT8u5kFTrMvAjtX4O5kA==", "dev": true, "dependencies": { "@eslint-community/regexpp": "^4.5.1", - "@typescript-eslint/scope-manager": "6.12.0", - "@typescript-eslint/type-utils": "6.12.0", - "@typescript-eslint/utils": "6.12.0", - "@typescript-eslint/visitor-keys": "6.12.0", + "@typescript-eslint/scope-manager": "6.13.1", + "@typescript-eslint/type-utils": "6.13.1", + "@typescript-eslint/utils": "6.13.1", + "@typescript-eslint/visitor-keys": "6.13.1", "debug": "^4.3.4", "graphemer": "^1.4.0", "ignore": "^5.2.4", @@ -619,15 +619,15 @@ } }, "node_modules/@typescript-eslint/parser": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-6.12.0.tgz", - "integrity": "sha512-s8/jNFPKPNRmXEnNXfuo1gemBdVmpQsK1pcu+QIvuNJuhFzGrpD7WjOcvDc/+uEdfzSYpNu7U/+MmbScjoQ6vg==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-6.13.1.tgz", + "integrity": "sha512-fs2XOhWCzRhqMmQf0eicLa/CWSaYss2feXsy7xBD/pLyWke/jCIVc2s1ikEAtSW7ina1HNhv7kONoEfVNEcdDQ==", "dev": true, "dependencies": { - "@typescript-eslint/scope-manager": "6.12.0", - "@typescript-eslint/types": "6.12.0", - "@typescript-eslint/typescript-estree": "6.12.0", - "@typescript-eslint/visitor-keys": "6.12.0", + "@typescript-eslint/scope-manager": "6.13.1", + "@typescript-eslint/types": "6.13.1", + "@typescript-eslint/typescript-estree": "6.13.1", + "@typescript-eslint/visitor-keys": "6.13.1", "debug": "^4.3.4" }, "engines": { @@ -647,13 +647,13 @@ } }, "node_modules/@typescript-eslint/scope-manager": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.12.0.tgz", - "integrity": "sha512-5gUvjg+XdSj8pcetdL9eXJzQNTl3RD7LgUiYTl8Aabdi8hFkaGSYnaS6BLc0BGNaDH+tVzVwmKtWvu0jLgWVbw==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.13.1.tgz", + "integrity": "sha512-BW0kJ7ceiKi56GbT2KKzZzN+nDxzQK2DS6x0PiSMPjciPgd/JRQGMibyaN2cPt2cAvuoH0oNvn2fwonHI+4QUQ==", "dev": true, "dependencies": { - "@typescript-eslint/types": "6.12.0", - "@typescript-eslint/visitor-keys": "6.12.0" + "@typescript-eslint/types": "6.13.1", + "@typescript-eslint/visitor-keys": "6.13.1" }, "engines": { "node": "^16.0.0 || >=18.0.0" @@ -664,13 +664,13 @@ } }, "node_modules/@typescript-eslint/type-utils": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-6.12.0.tgz", - "integrity": "sha512-WWmRXxhm1X8Wlquj+MhsAG4dU/Blvf1xDgGaYCzfvStP2NwPQh6KBvCDbiOEvaE0filhranjIlK/2fSTVwtBng==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-6.13.1.tgz", + "integrity": "sha512-A2qPlgpxx2v//3meMqQyB1qqTg1h1dJvzca7TugM3Yc2USDY+fsRBiojAEo92HO7f5hW5mjAUF6qobOPzlBCBQ==", "dev": true, "dependencies": { - "@typescript-eslint/typescript-estree": "6.12.0", - "@typescript-eslint/utils": "6.12.0", + "@typescript-eslint/typescript-estree": "6.13.1", + "@typescript-eslint/utils": "6.13.1", "debug": "^4.3.4", "ts-api-utils": "^1.0.1" }, @@ -691,9 +691,9 @@ } }, "node_modules/@typescript-eslint/types": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.12.0.tgz", - "integrity": "sha512-MA16p/+WxM5JG/F3RTpRIcuOghWO30//VEOvzubM8zuOOBYXsP+IfjoCXXiIfy2Ta8FRh9+IO9QLlaFQUU+10Q==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.13.1.tgz", + "integrity": "sha512-gjeEskSmiEKKFIbnhDXUyiqVma1gRCQNbVZ1C8q7Zjcxh3WZMbzWVfGE9rHfWd1msQtPS0BVD9Jz9jded44eKg==", "dev": true, "engines": { "node": "^16.0.0 || >=18.0.0" @@ -704,13 +704,13 @@ } }, "node_modules/@typescript-eslint/typescript-estree": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.12.0.tgz", - "integrity": "sha512-vw9E2P9+3UUWzhgjyyVczLWxZ3GuQNT7QpnIY3o5OMeLO/c8oHljGc8ZpryBMIyympiAAaKgw9e5Hl9dCWFOYw==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.13.1.tgz", + "integrity": "sha512-sBLQsvOC0Q7LGcUHO5qpG1HxRgePbT6wwqOiGLpR8uOJvPJbfs0mW3jPA3ujsDvfiVwVlWUDESNXv44KtINkUQ==", "dev": true, "dependencies": { - "@typescript-eslint/types": "6.12.0", - "@typescript-eslint/visitor-keys": "6.12.0", + "@typescript-eslint/types": "6.13.1", + "@typescript-eslint/visitor-keys": "6.13.1", "debug": "^4.3.4", "globby": "^11.1.0", "is-glob": "^4.0.3", @@ -731,17 +731,17 @@ } }, "node_modules/@typescript-eslint/utils": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.12.0.tgz", - "integrity": "sha512-LywPm8h3tGEbgfyjYnu3dauZ0U7R60m+miXgKcZS8c7QALO9uWJdvNoP+duKTk2XMWc7/Q3d/QiCuLN9X6SWyQ==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.13.1.tgz", + "integrity": "sha512-ouPn/zVoan92JgAegesTXDB/oUp6BP1v8WpfYcqh649ejNc9Qv+B4FF2Ff626kO1xg0wWwwG48lAJ4JuesgdOw==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.4.0", "@types/json-schema": "^7.0.12", "@types/semver": "^7.5.0", - "@typescript-eslint/scope-manager": "6.12.0", - "@typescript-eslint/types": "6.12.0", - "@typescript-eslint/typescript-estree": "6.12.0", + "@typescript-eslint/scope-manager": "6.13.1", + "@typescript-eslint/types": "6.13.1", + "@typescript-eslint/typescript-estree": "6.13.1", "semver": "^7.5.4" }, "engines": { @@ -756,12 +756,12 @@ } }, "node_modules/@typescript-eslint/visitor-keys": { - "version": "6.12.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.12.0.tgz", - "integrity": "sha512-rg3BizTZHF1k3ipn8gfrzDXXSFKyOEB5zxYXInQ6z0hUvmQlhaZQzK+YmHmNViMA9HzW5Q9+bPPt90bU6GQwyw==", + "version": "6.13.1", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.13.1.tgz", + "integrity": "sha512-NDhQUy2tg6XGNBGDRm1XybOHSia8mcXmlbKWoQP+nm1BIIMxa55shyJfZkHpEBN62KNPLrocSM2PdPcaLgDKMQ==", "dev": true, "dependencies": { - "@typescript-eslint/types": "6.12.0", + "@typescript-eslint/types": "6.13.1", "eslint-visitor-keys": "^3.4.1" }, "engines": { @@ -916,9 +916,9 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==" }, "node_modules/big-integer": { - "version": "1.6.51", - "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.51.tgz", - "integrity": "sha512-GPEid2Y9QU1Exl1rpO9B2IPJGHPSupF5GnVIP0blYvNOMer2bTvSWs1jGOUg04hTmu67nmLsQ9TBo1puaotBHg==", + "version": "1.6.52", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.52.tgz", + "integrity": "sha512-QxD8cf2eVqJOOz63z6JIN9BzvVs/dlySa5HGSBH5xtR8dPteIRQnBxxKqkNTiT6jbDTF6jAfrd4oMcND9RGbQg==", "dev": true, "engines": { "node": ">=0.6" @@ -1164,9 +1164,9 @@ } }, "node_modules/discord-api-types": { - "version": "0.37.64", - "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.64.tgz", - "integrity": "sha512-9aS+QuoNj+4e9d5uDKfds1DCpQLYn/mHx+M8OFHZ/ZZJVadZJEo275uBOaSsw5KGYGsZ4hxMzlOkIxnWirgqKA==" + "version": "0.37.65", + "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.65.tgz", + "integrity": "sha512-CQHW3Nu04LEHIj1Xps/sfGhTdrowilxnek2tirpLhwvrmgmLr1C6A+4JFLs+0kJMH2IX2QgDyA9GfNehqN+xPQ==" }, "node_modules/discord.js": { "version": "14.14.1", @@ -2155,9 +2155,9 @@ "integrity": "sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==" }, "node_modules/lru-cache": { - "version": "10.0.3", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.0.3.tgz", - "integrity": "sha512-B7gr+F6MkqB3uzINHXNctGieGsRTMwIBgxkp0yq/5BwcuDzD4A8wQpHQW6vDAm1uKSLQghmRdD9sKqf2vJ1cEg==", + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.1.0.tgz", + "integrity": "sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==", "dev": true, "engines": { "node": "14 || >=16.14" diff --git a/package.json b/package.json index 08c3a76..88799f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eximiabots-radiox", - "version": "0.5.2", + "version": "0.5.3", "description": "Internet Radio to your Discord guild", "main": "index.js", "scripts": { @@ -22,17 +22,17 @@ "@discordjs/opus": "^0.9.0", "@discordjs/rest": "^2.2.0", "@discordjs/voice": "^0.16.1", - "discord-api-types": "^0.37.63", + "discord-api-types": "^0.37.65", "discord.js": "^14.14.1", "dotenv": "^16.3.1", "libsodium-wrappers": "^0.7.13", "path": "^0.12.7" }, "devDependencies": { - "@types/node": "^20.9.4", + "@types/node": "^20.10.0", "@types/ws": "^8.5.9", - "@typescript-eslint/eslint-plugin": "^6.10.0", - "@typescript-eslint/parser": "^6.11.0", + "@typescript-eslint/eslint-plugin": "^6.13.1", + "@typescript-eslint/parser": "^6.13.1", "eslint": "^8.54.0", "eslint-config-prettier": "^9.0.0", "eslint-plugin-prettier": "^5.0.1", diff --git a/src/client/classes/Radio.ts b/src/client/classes/Radio.ts index 357c120..64c9743 100644 --- a/src/client/classes/Radio.ts +++ b/src/client/classes/Radio.ts @@ -1,19 +1,20 @@ -import { Channel, Collection, GuildMember, OAuth2Guild, TextBasedChannel, VoiceBasedChannel, VoiceChannel } from "discord.js"; +import { Collection, GuildMember, Message, OAuth2Guild, TextBasedChannel, VoiceBasedChannel, VoiceChannel } from "discord.js"; import { getVoiceConnection, joinVoiceChannel, VoiceConnection } from "@discordjs/voice"; import RadioClient from "../../Client"; import { station } from "./Stations"; import { datastore } from "./Datastore"; export interface radio { - textChannel: Channel | TextBasedChannel | undefined | null, - voiceChannel: Channel | VoiceBasedChannel | undefined, + textChannel: TextBasedChannel | undefined | null, + voiceChannel: VoiceBasedChannel | undefined, connection: VoiceConnection | null, - message: null, + message: Message | null, station: station, datastore?: datastore, currentTime?: number, startTime: number, playTime?: number, + guild?: any } export interface state { @@ -28,7 +29,7 @@ export interface state { } } -export default class Radio extends Map { +export default class Radio extends Map { constructor() { super(); @@ -75,8 +76,8 @@ export default class Radio extends Map { let date = new Date(); const construct: radio = { - textChannel: client.channels.cache.get(state.channels.text), - voiceChannel: client.channels.cache.get(state.channels.voice), + textChannel: client.channels.cache.get(state.channels.text) as TextBasedChannel, + voiceChannel: client.channels.cache.get(state.channels.voice) as VoiceBasedChannel, connection: null, message: null, station: station, diff --git a/src/client/classes/Stations.ts b/src/client/classes/Stations.ts index 225a2eb..c3f2fb5 100644 --- a/src/client/classes/Stations.ts +++ b/src/client/classes/Stations.ts @@ -8,9 +8,10 @@ export interface station { [key: string]: string }, playlist?: { - type: "supla" | "yle", + type: "radioplay" | "supla" | "yle", address: string | string } + track?: string; } export default class Stations extends Array { diff --git a/src/client/commands.ts b/src/client/commands.ts index 5874489..56814a3 100644 --- a/src/client/commands.ts +++ b/src/client/commands.ts @@ -6,7 +6,6 @@ import invite from "./commands/invite"; import list from "./commands/list"; import maintenance from "./commands/maintenance"; import next from "./commands/next"; -import nowplaying from "./commands/nowplaying"; import play from "./commands/play"; import prev from "./commands/prev"; import statistics from "./commands/statistics"; @@ -22,7 +21,7 @@ export interface command { } export default async function commands(client: RadioClient) { - const commands : command[] = [ bug, help, invite, list, maintenance, next, nowplaying, play, prev, statistics, status, stop ]; + const commands : command[] = [ bug, help, invite, list, maintenance, next, play, prev, statistics, status, stop ]; for(const command of commands){ client.commands.set(command.name, command); diff --git a/src/client/commands/list.ts b/src/client/commands/list.ts index 563d418..f2a0c3d 100644 --- a/src/client/commands/list.ts +++ b/src/client/commands/list.ts @@ -1,10 +1,13 @@ -import { ButtonInteraction, ChatInputCommandInteraction, EmbedBuilder, StringSelectMenuInteraction } from "discord.js"; +import { ApplicationCommandOptionType, ButtonInteraction, ChatInputCommandInteraction, EmbedBuilder, StringSelectMenuInteraction } from "discord.js"; import RadioClient from "../../Client"; import { station } from "../classes/Stations"; export default { name: 'list', - description: 'List radio stations', + description: 'List stations', + options: [ + { type: ApplicationCommandOptionType.String, name: "query", description: "Select list", choices: [{"name": "1", "value": "1"},{"name": "2", "value": "2"}], required: false} + ], category: 'radio', execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient) { @@ -15,6 +18,20 @@ export default { }); } + if(!interaction.guild) return; + + let query: string | null = null; + + if(interaction.isChatInputCommand()){ + query = interaction.options?.getString("query"); + } + + if(interaction.isStringSelectMenu()){ + query = interaction.values?.[0]; + } + + if(!query) query = "1"; + if(!client.stations) { return interaction.reply({ content: client.messages.emojis["error"] + client.messages.replace(client.messages.errorToGetPlaylist, { @@ -24,10 +41,10 @@ export default { }); } - const radio = client.radio?.get(interaction.guild?.id); + const radio = client.radio?.get(interaction.guild.id); if(radio && !client.config.maintenanceMode){ - client.funcs.listStations(client, interaction); + client.funcs.listStations(client, interaction, query); } else { let stations = `${client.stations.map((s: station) => `**#** ${s.name}`).join('\n')}` const hashs = stations.split('**#**').length; diff --git a/src/client/commands/maintenance.ts b/src/client/commands/maintenance.ts index e255915..1c928e6 100644 --- a/src/client/commands/maintenance.ts +++ b/src/client/commands/maintenance.ts @@ -183,7 +183,7 @@ export default { client.user?.setStatus('idle'); client.radio?.save(client); - setInterval(() => { + let timer : NodeJS.Timeout = setInterval(() => { if(client.radio?.size == 0 && client.config.streamerMode == "manual" && client.config.maintenanceMode){ client.streamer?.leave(client); client.streamer = new Streamer(); @@ -195,9 +195,9 @@ export default { } if(!client.config.maintenanceMode){ - clearInterval(undefined); + clearInterval(timer); } - }, 500); + }, 1000); break; case "11": @@ -207,7 +207,7 @@ export default { client.user?.setStatus('idle'); client.radio?.save(client); - setInterval(() => { + let timer2 : NodeJS.Timeout = setInterval(() => { if(client.radio?.size == 0 && client.config.streamerMode == "auto" && client.config.maintenanceMode){ client.streamer?.leave(client); client.streamer = new Streamer(); @@ -219,9 +219,9 @@ export default { } if(!client.config.maintenanceMode){ - clearInterval(undefined); + clearInterval(timer2); } - }, 500); + }, 1000); break; default: diff --git a/src/client/commands/next.ts b/src/client/commands/next.ts index 60dfd46..9841603 100644 --- a/src/client/commands/next.ts +++ b/src/client/commands/next.ts @@ -9,7 +9,9 @@ export default { category: 'radio', async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: command) { if (client.funcs.check(client, interaction, command)) { + if(!interaction.guild) return; const radio = client.radio?.get(interaction.guild?.id); + if(!radio) return; if(client.config.maintenanceMode){ return interaction.reply({ diff --git a/src/client/commands/nowplaying.ts b/src/client/commands/nowplaying.ts deleted file mode 100644 index 4bfcf97..0000000 --- a/src/client/commands/nowplaying.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { ButtonInteraction, ChatInputCommandInteraction, EmbedBuilder, StringSelectMenuInteraction } from "discord.js"; -import RadioClient from "../../Client"; -import { command } from "../commands"; - -export default { - name: 'nowplaying', - description: 'Current Radio Station', - category: 'radio', - async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: command) { - if(client.funcs.check(client, interaction, command)) { - - const radio = client.radio?.get(interaction.guild?.id); - - let date = new Date(); - radio.currentTime = date.getTime(); - radio.playTime = parseInt(radio.currentTime)-parseInt(radio.startTime); - const completed = (radio.playTime); - - 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; - break; - case "yle": - radio.station.track = "-"; - break; - default: - radio.station.track = "-"; - } - } catch(TypeError) { - - } - } - - const embed = new EmbedBuilder() - .setTitle(client.messages.nowplayingTitle) - .setThumbnail((radio.station.logo || "https://cdn.discordapp.com/emojis/" + client.messages.emojis["play"].replace(/[^0-9]+/g, ''))) - .setColor(client.config.embedColor) - .setDescription(client.messages.replace(client.messages.nowplayingDescription, { - "%radio.station.name%": radio.station.name, - "%radio.station.owner%\n": radio.station.name != radio.station.owner ? radio.station.owner + "\n" : "", - "%client.funcs.msToTime(completed)%": client.funcs.msToTime(completed), - "\n\n%radio.station.track%": radio.station.track != undefined ? "\n\n" + radio.station.track : "" - })) - .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, '') - }); - - interaction.reply({ - embeds: [embed], - ephemeral: true - }); - } - } -}; diff --git a/src/client/commands/play.ts b/src/client/commands/play.ts index e9bb0cc..3b4545e 100644 --- a/src/client/commands/play.ts +++ b/src/client/commands/play.ts @@ -13,6 +13,8 @@ export default { category: "radio", async execute(interaction: ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient) { + if(!interaction.guild) return; + if(client.config.maintenanceMode){ return interaction.reply({ content: client.messages.emojis["error"] + client.messages.maintenance, @@ -40,10 +42,10 @@ export default { } if(!query){ - return client.funcs.listStations(client, interaction); + return client.funcs.listStations(client, interaction, "1"); } - const radio = client.radio?.get(interaction.guild?.id); + const radio = client.radio?.get(interaction.guild.id); if(!(interaction.member instanceof GuildMember)) return; const voiceChannel = interaction.member?.voice.channel; diff --git a/src/client/commands/prev.ts b/src/client/commands/prev.ts index fa8e39c..05191a6 100644 --- a/src/client/commands/prev.ts +++ b/src/client/commands/prev.ts @@ -9,7 +9,9 @@ export default { category: 'radio', async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: command) { if (client.funcs.check(client, interaction, command)) { + if(!interaction.guild) return; const radio = client.radio?.get(interaction.guild?.id); + if(!radio) return; if(client.config.maintenanceMode){ return interaction.reply({ diff --git a/src/client/commands/stop.ts b/src/client/commands/stop.ts index aa15563..3b405a3 100644 --- a/src/client/commands/stop.ts +++ b/src/client/commands/stop.ts @@ -8,7 +8,9 @@ export default { category: 'radio', async execute(interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, client: RadioClient, command: command) { if (client.funcs.check(client, interaction, command)) { + if(!interaction.guild) return; const radio = client.radio?.get(interaction.guild?.id); + if(!radio) return; client.statistics?.update(client, interaction.guild, radio); radio.connection?.destroy(); client.funcs.logger('Radio', interaction.guild?.id + " / " + 'Stop'); @@ -18,7 +20,7 @@ export default { .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messages.emojis["stop"].replace(/[^0-9]+/g, '')) .setColor(client.config.embedColor) .addFields({ - name: client.messages.nowplayingTitle, + name: client.messages.playTitle1, value: "-" }) .setImage('https://waren.io/berriabot-temp-sa7a36a9xm6837br/images/empty-3.png') @@ -28,9 +30,9 @@ export default { }); if(!radio.message){ - radio.message = radio.textChannel.send({ embeds: [embed], components: [] }); + radio.message = await radio.textChannel?.send({ embeds: [embed], components: [] }) ?? null; } else { - if(radio.textChannel.id == radio.message.channel.id){ + if(radio.textChannel?.id == radio.message.channel.id){ radio.message.edit({ embeds: [embed], components: [] }); } else { radio.message?.delete(); @@ -41,7 +43,7 @@ export default { await radio.message?.delete(); }, 5000); - client.radio?.delete(interaction.guild?.id); + client.radio?.delete(interaction.guild.id); interaction.reply({ content: client.messages.emojis["stop"] + client.messages.stop, diff --git a/src/client/events/voiceStateUpdate.ts b/src/client/events/voiceStateUpdate.ts index 8caad08..b848eec 100644 --- a/src/client/events/voiceStateUpdate.ts +++ b/src/client/events/voiceStateUpdate.ts @@ -53,7 +53,7 @@ export default async function voiceStateUpdate(client: RadioClient, oldState: Vo if ((oldState.channel.members.filter(member => !member.user.bot).size === 0 && oldState.channel === radio.voiceChannel) || change) { setTimeout(() => { if (!radio || !radio.connection || !radio.connection === null) return; - if (radio.voiceChannel.members.filter((member: GuildMember) => !member.user.bot).size === 0) { + if (radio.voiceChannel?.members.filter((member: GuildMember) => !member.user.bot).size === 0) { client.statistics?.update(client, newState.guild, radio); radio.connection?.destroy(); radio.message?.delete(); diff --git a/src/client/funcs/check.ts b/src/client/funcs/check.ts index 63b6252..2659599 100644 --- a/src/client/funcs/check.ts +++ b/src/client/funcs/check.ts @@ -4,6 +4,7 @@ import { command } from "../commands"; export default function check(client: RadioClient, interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, command: command) { + if(!interaction.guild) return; const radio = client.radio?.get(interaction.guild?.id); if(!client.stations) { interaction.reply({ diff --git a/src/client/funcs/listStations.ts b/src/client/funcs/listStations.ts index 96349a6..3b443c2 100644 --- a/src/client/funcs/listStations.ts +++ b/src/client/funcs/listStations.ts @@ -1,11 +1,12 @@ import { ActionRowBuilder, ButtonInteraction, ChatInputCommandInteraction, SelectMenuComponentOptionData, StringSelectMenuBuilder, StringSelectMenuInteraction } from "discord.js"; import RadioClient from "../../Client"; -export default function listStations(client: RadioClient, interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction){ +export default function listStations(client: RadioClient, interaction: ButtonInteraction | ChatInputCommandInteraction | StringSelectMenuInteraction, offset: string){ if(!client.stations) return; let options : SelectMenuComponentOptionData[] = new Array(); + for (const station of client.stations){ options.push({ label: station.name, @@ -14,6 +15,17 @@ export default function listStations(client: RadioClient, interaction: ButtonInt }); } + switch(offset){ + case "1": + options = options.slice(0,Math.round(options.length/2)); + break; + case "2": + options = options.slice(Math.round(options.length/2),options.length-1); + break; + default: + options = options.slice(0,Math.round(options.length/2)); + } + const menu = new ActionRowBuilder() .addComponents( new StringSelectMenuBuilder() diff --git a/src/client/funcs/play.ts b/src/client/funcs/play.ts index 283a7aa..a0864b9 100644 --- a/src/client/funcs/play.ts +++ b/src/client/funcs/play.ts @@ -6,29 +6,42 @@ export default async function play(client: RadioClient, interaction: ChatInputCo if(!guild) return; const radio = client.radio?.get(guild.id); + if(!radio) return; const audioPlayer = client.streamer?.listen(station); - radio.connection.subscribe(audioPlayer); + if(!audioPlayer) return; + 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) + if(radio.station.playlist){ + if(radio.station.playlist.type == "radioplay" || 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; - break; - case "yle": - radio.station.track = "-"; - break; - default: - radio.station.track = "-"; - } - } catch(TypeError) { + radio.station.track = "-"; + if(playlist){ + switch(radio.station.playlist.type){ + case "radioplay": + if(playlist[0] && playlist[0].stationNowPlaying && playlist[0].stationNowPlaying.nowPlayingArtist && playlist[0].stationNowPlaying.nowPlayingTrack){ + radio.station.track = "__" + playlist[0].stationNowPlaying.nowPlayingArtist + "__" + "\n" + playlist[0].stationNowPlaying.nowPlayingTrack; + } + break; + case "supla": + if(playlist.items && playlist.items[0] && playlist.items[0].artist && playlist.items[0].song){ + radio.station.track = "__" + playlist.items[0].artist + "__" + "\n" + playlist.items[0].song; + } + break; + case "yle": + if(playlist.data && playlist.data.performer && playlist.data.title){ + radio.station.track = "__" + playlist.data.performer + "__" + "\n" + playlist.data.title; + } + break; + default: + radio.station.track = "-"; + } + } } } @@ -48,7 +61,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, @@ -88,80 +108,104 @@ export default async function play(client: RadioClient, interaction: ChatInputCo ); if(!radio.message){ - radio.message = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }); + radio.message = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }) ?? null; } else { - if(radio.textChannel.id == radio.message.channel.id){ + 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 = await radio.textChannel?.send({ embeds: [embed], components: [buttons] }) ?? null; } } - setInterval(async function(){ - let changed = false; + const oldRadio = {...radio}; - if(radio.station?.playlist?.type == "supla" || radio.station?.playlist?.type == "yle"){ - let playlist: any = await fetch(radio.station.playlist.address) + let timer : NodeJS.Timeout = setInterval(async function(){ + const radio = client.radio?.get(guild.id); + + if(!radio || !oldRadio || radio.station.name != oldRadio.station.name) { + return clearInterval(timer); + } + + if(radio.station.playlist){ + if(radio.station.playlist.type == "radioplay" || 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; - radio.station.track = "__" + playlist.items[0].artist + "__" + "\n" + playlist.items[0].song; - } - break; - case "yle": - radio.station.track = "-"; - break; - default: - radio.station.track = "-"; - } - } catch(TypeError) { + radio.station.track = "-"; + + if(playlist){ + switch(radio.station.playlist?.type){ + case "radioplay": + if(playlist[0] && playlist[0].stationNowPlaying && playlist[0].stationNowPlaying.nowPlayingArtist && playlist[0].stationNowPlaying.nowPlayingTrack){ + radio.station.track = "__" + playlist[0].stationNowPlaying.nowPlayingArtist + "__" + "\n" + playlist[0].stationNowPlaying.nowPlayingTrack; + } + break; + case "supla": + if(playlist.items && playlist.items[0] && playlist.items[0].artist && playlist.items[0].song){ + radio.station.track = "__" + playlist.items[0].artist + "__" + "\n" + playlist.items[0].song; + } + break; + case "yle": + if(playlist.data && playlist.data.performer && playlist.data.title){ + radio.station.track = "__" + playlist.data.performer + "__" + "\n" + playlist.data.title; + } + break; + default: + radio.station.track = "-"; + } + } } } - 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 = radio.currentTime - 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] }) ?? null; + } 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] }) ?? null; } } - },15000); + },2500); interaction?.reply({ content: client.messages.emojis["play"] + client.messages.replace(client.messages.play, { 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.",