From 24b78c193a32fc14dac19751c4957a2f7d57b706 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Sun, 30 Jan 2022 20:10:47 +0700 Subject: [PATCH] Create Leave.ts --- src/discord/MusicPlayer/Leave.ts | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/discord/MusicPlayer/Leave.ts diff --git a/src/discord/MusicPlayer/Leave.ts b/src/discord/MusicPlayer/Leave.ts new file mode 100644 index 0000000..64a4138 --- /dev/null +++ b/src/discord/MusicPlayer/Leave.ts @@ -0,0 +1,55 @@ +import { Message, CommandInteraction, Interaction, VoiceChannel } from "discord.js"; +import { getEmotes, makeSuccessEmbed, makeProcessingEmbed, sendMessage, sendMessageOrInteractionResponse, makeInfoEmbed } from "../../utils/DiscordMessage"; +import DiscordProvider from "../../providers/Discord"; +import Users from "../../services/Users" +import Environment from "../../providers/Environment"; +import DiscordMusicPlayer, { DiscordMusicPlayerInstance } from "../../providers/DiscordMusicPlayer"; + +const EMBEDS = { + VOICECHANNEL_LEFT: (data: Message | Interaction) => { + return makeSuccessEmbed({ + title: `Success`, + description: `Left the voice channel`, + user: (data instanceof Interaction) ? data.user : data.author + }); + } +} + +export default class Leave { + + async onCommand(command: string, args: any, message: Message) { + if(command.toLowerCase() !== 'leave') return; + await this.process(message, args); + } + + async interactionCreate(interaction: CommandInteraction) { + if(interaction.isCommand()) { + if(typeof interaction.commandName === 'undefined') return; + if((interaction.commandName).toLowerCase() !== 'leave') return; + await this.process(interaction, interaction.options); + } + } + + async process(data: Interaction | Message, args: any) { + const isSlashCommand = data instanceof CommandInteraction && data.isCommand(); + const isMessage = data instanceof Message; + + if(!isSlashCommand && !isMessage) return; + + if(!data.member) return; + if(!data.guildId) return; + + const channel: any = isMessage ? data.member.voice.channel : DiscordProvider.client.guilds.cache.get((data as Interaction).guildId!)!.members.cache.get((data as Interaction).user.id)?.voice.channel; + if(!channel) return; + + + let instance = DiscordMusicPlayer.getGuildInstance(data.guildId); + if(!instance) return; + + + DiscordMusicPlayer.destoryGuildInstance(data.guild?.id!); + await sendMessageOrInteractionResponse(data, { embeds:[EMBEDS.VOICECHANNEL_LEFT(data)] }); + + return; + } +} \ No newline at end of file