From 0b7da1937b2aefaa303c1d375e59a8b89f1877d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christer=20War=C3=A9n?= Date: Fri, 27 Aug 2021 01:38:33 +0300 Subject: [PATCH] Removed createDiscordJSAdapter --- src/client/commands/play.js | 38 ++++++++++++--- src/client/events/voiceStateUpdate.js | 3 +- src/client/utils/adapter.ts | 70 --------------------------- 3 files changed, 32 insertions(+), 79 deletions(-) delete mode 100644 src/client/utils/adapter.ts diff --git a/src/client/commands/play.js b/src/client/commands/play.js index 553c307..f77a82e 100644 --- a/src/client/commands/play.js +++ b/src/client/commands/play.js @@ -4,7 +4,6 @@ const { getVoiceConnection, joinVoiceChannel } = require("@discordjs/voice"); -const { createDiscordJSAdapter } = require("../utils/adapter"); module.exports = { name: "play", @@ -92,7 +91,7 @@ module.exports = { radio.station = station; radio.textChannel = interaction.channel; - play(interaction, interaction.guild, client, url); + play(interaction, interaction.guild, client, url, Discord); return; } @@ -101,6 +100,7 @@ module.exports = { textChannel: interaction.channel, voiceChannel: voiceChannel, connection: null, + message: null, audioPlayer: createAudioPlayer(), station: station }; @@ -112,12 +112,12 @@ module.exports = { joinVoiceChannel({ channelId: voiceChannel.id, guildId: voiceChannel.guild.id, - adapterCreator: createDiscordJSAdapter(voiceChannel) + adapterCreator: voiceChannel.guild.voiceAdapterCreator }); construct.connection = connection; let date = new Date(); construct.startTime = date.getTime(); - play(interaction, interaction.guild, client, url); + play(interaction, interaction.guild, client, url, Discord); client.datastore.checkEntry(interaction.guild.id); construct.currentGuild = client.datastore.getEntry(interaction.guild.id); @@ -136,7 +136,7 @@ module.exports = { } }; -function play(interaction, guild, client, url) { +async function play(interaction, guild, client, url, Discord) { let message = {}; const radio = client.radio.get(guild.id); const resource = createAudioResource(url); @@ -163,8 +163,32 @@ function play(interaction, guild, client, url) { return interaction.reply(client.messages.errorPlaying); }); - message.play = client.messages.play.replace("%radio.station.name%", radio.station.name); - interaction.reply(client.messageEmojis["play"] + message.play); + 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)%", "--:--"); + + const embed = new Discord.MessageEmbed() + .setTitle("RadioX") + .setThumbnail("https://cdn.discordapp.com/emojis/" + client.messageEmojis["play"].replace(/[^0-9]+/g, '')) + .setColor(client.config.embedColor) + .setDescription(message.nowplayingDescription) + .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] }); + } else { + radio.message.edit({ embeds: [embed] }); + } + + setTimeout(function() { + message.play = client.messages.play.replace("%radio.station.name%", radio.station.name); + + interaction.reply({ + content: client.messageEmojis["play"] + message.play, + ephemeral: true + }); + }, 1500) + } function searchStation(key, client) { diff --git a/src/client/events/voiceStateUpdate.js b/src/client/events/voiceStateUpdate.js index e1809d5..1ae0fa1 100644 --- a/src/client/events/voiceStateUpdate.js +++ b/src/client/events/voiceStateUpdate.js @@ -2,7 +2,6 @@ const { getVoiceConnection, joinVoiceChannel } = require("@discordjs/voice"); -const { createDiscordJSAdapter } = require("../utils/adapter"); module.exports = { name: "voiceStateUpdate", @@ -30,7 +29,7 @@ module.exports = { radio.connection = joinVoiceChannel({ channelId: oldState.channel.id, guildId: oldState.channel.guild.id, - adapterCreator: createDiscordJSAdapter(oldState.channel) + adapterCreator: oldState.channel.guild.voiceAdapterCreator }) //radio.connection = await oldState.channel.join() ), diff --git a/src/client/utils/adapter.ts b/src/client/utils/adapter.ts deleted file mode 100644 index b008511..0000000 --- a/src/client/utils/adapter.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from '@discordjs/voice'; -import { VoiceChannel, Snowflake, Client, Constants, WebSocketShard, Guild, StageChannel } from 'discord.js'; -import { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v9'; - -const adapters = new Map(); -const trackedClients = new Set(); - -/** - * Tracks a Discord.js client, listening to VOICE_SERVER_UPDATE and VOICE_STATE_UPDATE events. - * @param client - The Discord.js Client to track - */ -function trackClient(client: Client) { - if (trackedClients.has(client)) return; - trackedClients.add(client); - client.ws.on(Constants.WSEvents.VOICE_SERVER_UPDATE, (payload: GatewayVoiceServerUpdateDispatchData) => { - adapters.get(payload.guild_id)?.onVoiceServerUpdate(payload); - }); - client.ws.on(Constants.WSEvents.VOICE_STATE_UPDATE, (payload: GatewayVoiceStateUpdateDispatchData) => { - if (payload.guild_id && payload.session_id && payload.user_id === client.user?.id) { - adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload); - } - }); -} - -const trackedGuilds = new Map>(); - -function cleanupGuilds(shard: WebSocketShard) { - const guilds = trackedGuilds.get(shard); - if (guilds) { - for (const guildID of guilds.values()) { - adapters.get(guildID)?.destroy(); - } - } -} - -function trackGuild(guild: Guild) { - let guilds = trackedGuilds.get(guild.shard); - if (!guilds) { - const cleanup = () => cleanupGuilds(guild.shard); - guild.shard.on('close', cleanup); - guild.shard.on('destroyed', cleanup); - guilds = new Set(); - trackedGuilds.set(guild.shard, guilds); - } - guilds.add(guild.id); -} - -/** - * Creates an adapter for a Voice Channel - * @param channel - The channel to create the adapter for - */ -export function createDiscordJSAdapter(channel: VoiceChannel | StageChannel): DiscordGatewayAdapterCreator { - return (methods) => { - adapters.set(channel.guild.id, methods); - trackClient(channel.client); - trackGuild(channel.guild); - return { - sendPayload(data) { - if (channel.guild.shard.status === Constants.Status.READY) { - channel.guild.shard.send(data); - return true; - } - return false; - }, - destroy() { - return adapters.delete(channel.guild.id); - }, - }; - }; -}