From d1e4f2cf93c6ff9ed545155bd63e293f008bdcf3 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Thu, 3 Mar 2022 22:18:34 +0700 Subject: [PATCH] Add Debug Commands --- src/discord/developer/Debug.ts | 137 ++++++++++++++++++++++++++++ src/discord/developer/FakeError.ts | 84 ----------------- src/providers/Discord.ts | 4 +- src/providers/DiscordMusicPlayer.ts | 5 +- 4 files changed, 143 insertions(+), 87 deletions(-) create mode 100644 src/discord/developer/Debug.ts delete mode 100644 src/discord/developer/FakeError.ts diff --git a/src/discord/developer/Debug.ts b/src/discord/developer/Debug.ts new file mode 100644 index 0000000..73ac5e1 --- /dev/null +++ b/src/discord/developer/Debug.ts @@ -0,0 +1,137 @@ +import DiscordModule, { HybridInteractionMessage } from "../../utils/DiscordModule"; + +import { Message, Interaction, CommandInteraction, MessageActionRow, MessageButton, ButtonInteraction } from "discord.js"; +import { makeInfoEmbed, makeErrorEmbed, sendHybridInteractionMessageResponse, sendMessageOrInteractionResponse } from "../../utils/DiscordMessage"; +import DiscordProvider from "../../providers/Discord"; +import DiscordMusicPlayer from "../../providers/DiscordMusicPlayer"; +import Users from "../../services/Users"; + +const EMBEDS = { + DEBUG_INFO: (data: Message | Interaction) => { + return makeInfoEmbed({ + title: 'Debug', + description: `Test and debug something. **Should not be used on production**`, + fields: [ + { + name: 'Available arguments', + value: '``invalidInteraction`` ``crashMusicPlayer`` ``crash``' + } + ], + user: (data instanceof Interaction) ? data.user : data.author + }); + }, + INVALID_TEST: (data: Message | Interaction) => { + return makeInfoEmbed({ + title: 'Click button below to test invalid interaction', + description: `The interaction will be failed`, + user: (data instanceof Interaction) ? data.user : data.author + }); + }, + CRASHING: (data: Message | Interaction) => { + return makeInfoEmbed({ + icon: '💀', + title: 'Crashing myself', + description: `Sayonara.... cruel world`, + user: (data instanceof Interaction) ? data.user : data.author + }); + }, + NOT_DEVELOPER: (data: Message | Interaction) => { + return makeErrorEmbed({ + title: 'Developer only', + description: `This command is restricted to the developers only`, + user: (data instanceof Interaction) ? data.user : data.author + }); + }, +} + +export default class Debug extends DiscordModule { + + public id = "Discord_Developer_Debug"; + public commands = ["debug", "dbg"]; + public commandInteractionName = "debug"; + + async GuildOnModuleCommand(args: any, message: Message) { + await this.run(new HybridInteractionMessage(message), args); + } + + async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) { + await this.run(new HybridInteractionMessage(interaction), interaction.options); + } + + async GuildButtonInteractionCreate(data: ButtonInteraction) { + if(data.customId !== "dev_make_invalid_interaction") return; + + const hybridData = new HybridInteractionMessage(data); + const user = hybridData.getUser(); + + if(!user) return; + + if (!Users.isDeveloper(user.id)) + return await sendHybridInteractionMessageResponse(hybridData, { embeds: [EMBEDS.NOT_DEVELOPER(hybridData.getRaw())] }); + + setTimeout(async () => { + await sendHybridInteractionMessageResponse(hybridData, { content: 'dev_make_invalid_interaction' }); + }, 7 * 1000) + } + + async run(data: HybridInteractionMessage, args: any) { + + const guild = data.getGuild(); + const user = data.getUser(); + const channel = data.getChannel(); + + if(!guild || !user || !channel) return; + + if (!Users.isDeveloper(user.id)) + return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NOT_DEVELOPER(data.getRaw())] }); + + const funct = { + crash: async (data: HybridInteractionMessage) => { + await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.CRASHING(data.getRaw())] }); + throw new Error("Manually crashed by debug command"); + }, + crashMusicPlayer: async (data: HybridInteractionMessage) => { + const instance = DiscordMusicPlayer.getGuildInstance(guild.id); + if(!instance) return; + + instance._fake_error_on_player(); + }, + invalidInteraction: async (data: HybridInteractionMessage) => { + + const row = new MessageActionRow() + .addComponents( + new MessageButton() + .setEmoji('😥') + .setLabel(' Make invalid interaction (Wait 7 seconds, check error in console or logs)') + .setCustomId('dev_make_invalid_interaction') + .setStyle('PRIMARY'), + ) + await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.INVALID_TEST(data.getRaw())], components: [row] }); + } + } + + let query; + + if (data.isMessage()) { + if (args.length === 0) + return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.DEBUG_INFO(data.getRaw())] }); + + query = args[0].toLowerCase(); + } + else if (data.isSlashCommand()) { + query = args.getSubcommand(); + } + + switch (query) { + case "info": + return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.DEBUG_INFO(data.getRaw())] }); + case "crash": + return await funct.crash(data); + case "invalidinteraction": + return await funct.invalidInteraction(data); + case "crashmusicplayer": + return await funct.crashMusicPlayer(data); + } + + } +} \ No newline at end of file diff --git a/src/discord/developer/FakeError.ts b/src/discord/developer/FakeError.ts deleted file mode 100644 index 227252d..0000000 --- a/src/discord/developer/FakeError.ts +++ /dev/null @@ -1,84 +0,0 @@ -import DiscordModule, { HybridInteractionMessage } from "../../utils/DiscordModule"; - -import { Message, Interaction, CommandInteraction } from "discord.js"; -import { makeInfoEmbed, makeErrorEmbed, sendHybridInteractionMessageResponse } from "../../utils/DiscordMessage"; -import DiscordMusicPlayer from "../../providers/DiscordMusicPlayer"; -import Users from "../../services/Users"; - -const EMBEDS = { - FAKEERROR_INFO: (data: Message | Interaction) => { - return makeInfoEmbed({ - title: 'Fake error', - description: `Simulate an error`, - fields: [ - { - name: 'Available arguments', - value: '``crashMusicPlayer``' - } - ], - user: (data instanceof Interaction) ? data.user : data.author - }); - }, - NOT_DEVELOPER: (data: Message | Interaction) => { - return makeErrorEmbed({ - title: 'Developer only', - description: `This command is restricted to the developers only`, - user: (data instanceof Interaction) ? data.user : data.author - }); - }, -} - -export default class FakeError extends DiscordModule { - - public id = "Discord_Developer_FakeError"; - public commands = ["fakeerror"]; - public commandInteractionName = "fakeerror"; - - async GuildOnModuleCommand(args: any, message: Message) { - await this.run(new HybridInteractionMessage(message), args); - } - - async GuildModuleCommandInteractionCreate(interaction: CommandInteraction) { - await this.run(new HybridInteractionMessage(interaction), interaction.options); - } - - async run(data: HybridInteractionMessage, args: any) { - - const guild = data.getGuild(); - const user = data.getUser(); - - if(!guild || !user) return; - - if (!Users.isDeveloper(user.id!)) - return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.NOT_DEVELOPER(data.getRaw())] }); - - const funct = { - crashMusicPlayer: async (data: HybridInteractionMessage) => { - const instance = DiscordMusicPlayer.getGuildInstance(guild.id); - if(!instance) return; - - instance._fake_error_on_player(); - } - } - - let query; - - if (data.isMessage()) { - if (args.length === 0) - return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.FAKEERROR_INFO(data.getRaw())] }); - - query = args[0].toLowerCase(); - } - else if (data.isSlashCommand()) { - query = args.getSubcommand(); - } - - switch (query) { - case "info": - return await sendHybridInteractionMessageResponse(data, { embeds: [EMBEDS.FAKEERROR_INFO(data.getRaw())] }); - case "crashmusicplayer": - return await funct.crashMusicPlayer(data); - } - - } -} \ No newline at end of file diff --git a/src/providers/Discord.ts b/src/providers/Discord.ts index 95638f5..d282ee7 100644 --- a/src/providers/Discord.ts +++ b/src/providers/Discord.ts @@ -25,7 +25,7 @@ import Discord_MusicPlayer_Search from "../discord/MusicPlayer/Search"; import Discord_MusicPlayer_NowPlaying from "../discord/MusicPlayer/NowPlaying"; import Discord_Developer_ServiceAnnouncement from "../discord/developer/ServiceAnnouncement"; -import Discord_Developer_FakeError from "../discord/developer/FakeError"; +import Discord_Developer_Debug from "../discord/developer/Debug"; import Cache from "./Cache"; import DiscordModule from "../utils/DiscordModule"; @@ -71,7 +71,7 @@ class Discord { new Discord_MembershipScreening(), new Discord_Developer_ServiceAnnouncement(), - new Discord_Developer_FakeError() + new Discord_Developer_Debug() ]; for(const _module of modules) { diff --git a/src/providers/DiscordMusicPlayer.ts b/src/providers/DiscordMusicPlayer.ts index e68a9be..c738f23 100644 --- a/src/providers/DiscordMusicPlayer.ts +++ b/src/providers/DiscordMusicPlayer.ts @@ -210,7 +210,10 @@ export class DiscordMusicPlayerInstance { } public async _fake_error_on_player() { - this.player.emit('error', new AudioPlayerError(new Error("fake error"), null!)); + const stream = 'https://fakestream:42069/fake/stream/fake/audio/fake.mp3'; + const resource = createAudioResource(stream); + this.player.play(resource); + this.player.emit('error', new AudioPlayerError(new Error("Music player was manually crashed"), null!)); } }