From e4e0a600565de5e41cc651f8bb7b5ee4b5e6dd82 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Sat, 7 Oct 2023 01:26:23 +1100 Subject: [PATCH] Create HookManager.ts --- src/commands/suggest.ts | 7 +++---- src/funcs/HookManager.ts | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 src/funcs/HookManager.ts diff --git a/src/commands/suggest.ts b/src/commands/suggest.ts index 8196e9a..1d61db7 100644 --- a/src/commands/suggest.ts +++ b/src/commands/suggest.ts @@ -1,6 +1,7 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import MessageTool from '../helpers/MessageTool.js'; +import HookMgr from '../funcs/HookManager.js'; export default { async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ const replyInDM = interaction.options.getString('message'); @@ -13,7 +14,6 @@ export default { const dmFail = `Failed to send a DM to ${MessageTool.formatMention(userid, 'user')}, they possibly have it turned off or blocked me.\nSuggestion ID: **${suggestionIDReply}**`; ({ your: async()=>{ - const webhook = await (await (client.channels.fetch(client.config.mainServer.channels.bot_suggestions) as Promise)).fetchWebhooks().then(x => x.find(y => y.name === client.user.username)); const suggestionText = interaction.options.getString('suggestion'); const suggestionImage = interaction.options.getAttachment('image'); const notifEmbed = new client.embed() @@ -26,11 +26,10 @@ export default { suggestionText )); if (suggestionImage) notifEmbed.setImage(suggestionImage.url); - webhook.send({embeds: [notifEmbed], username: `${client.user.username} Notification`, avatarURL: client.user.avatarURL({size: 256})} - ).catch(e=>{ + HookMgr.send(client, 'bot_suggestions', '1079621523561779272', {embeds:[notifEmbed], username: `${client.user.username} Suggestions`, avatarURL: client.user.avatarURL({size:256})}).catch(e=>{ console.log(e.message); interaction.reply({content: 'Failed to send suggestion, try again later.', ephemeral: true}) - }) + }); await client.suggestion._content.create({_id: suggestionID, idea: suggestionText, user: {_id: interaction.user.id, name: interaction.user.username}, state: 'Pending'}); interaction.reply({content: `Suggestion sent, here is your suggestion ID to take note of it: \`${suggestionID}\``, ephemeral: true}) }, diff --git a/src/funcs/HookManager.ts b/src/funcs/HookManager.ts new file mode 100644 index 0000000..998f280 --- /dev/null +++ b/src/funcs/HookManager.ts @@ -0,0 +1,21 @@ +import Discord from 'discord.js'; +import TClient from '../client.js'; +import {Config} from '../typings/interfaces'; +import {readFileSync} from 'node:fs'; +const config:Config = JSON.parse(readFileSync('src/config.json', 'utf-8')); +type ChannelList = keyof typeof config.mainServer.channels; +export default class HookMgr { + protected static async fetch(client:TClient, channel:ChannelList, webhookId:Discord.Snowflake) { + const hookInstance = await (await client.channels.fetch(config.mainServer.channels[channel]) as Discord.TextChannel).fetchWebhooks().then(x=>x.find(y=>y.id===webhookId)); + if (!hookInstance) throw new Error('[HookManager] Webhook not found.'); + return hookInstance; + } + static async send(client:TClient, channel:ChannelList, webhookId:Discord.Snowflake, message:string|Discord.MessagePayload|Discord.WebhookMessageCreateOptions) { + const hook = await this.fetch(client, channel, webhookId); + return hook.send(message).catch(err=>(client.channels.resolve(config.mainServer.channels.errors) as Discord.TextChannel).send(`Failed to send a webhook message in #${channel}:\n\`\`\`\n${err.message}\n\`\`\``)); + } + static async edit(client:TClient, channel:ChannelList, webhookId:Discord.Snowflake, messageId:Discord.Snowflake, message:string|Discord.MessagePayload|Discord.WebhookMessageEditOptions) { + const hook = await this.fetch(client, channel, webhookId); + return hook.editMessage(messageId, message).catch(err=>(client.channels.resolve(config.mainServer.channels.errors) as Discord.TextChannel).send(`Failed to edit a webhook message in <#${channel}>:\n\`\`\`\n${err.message}\n\`\`\``)); + } +}