1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-11-17 00:10:58 -05:00

Create HookManager.ts

This commit is contained in:
toast-ts 2023-10-07 01:26:23 +11:00
parent c2a7725546
commit 5d47d3506b
2 changed files with 24 additions and 4 deletions

View File

@ -1,6 +1,7 @@
import Discord from 'discord.js'; import Discord from 'discord.js';
import TClient from '../client.js'; import TClient from '../client.js';
import MessageTool from '../helpers/MessageTool.js'; import MessageTool from '../helpers/MessageTool.js';
import HookMgr from '../funcs/HookManager.js';
export default { export default {
async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){
const replyInDM = interaction.options.getString('message'); 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}**`; 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()=>{ your: async()=>{
const webhook = await (await (client.channels.fetch(client.config.mainServer.channels.bot_suggestions) as Promise<Discord.TextChannel>)).fetchWebhooks().then(x => x.find(y => y.name === client.user.username));
const suggestionText = interaction.options.getString('suggestion'); const suggestionText = interaction.options.getString('suggestion');
const suggestionImage = interaction.options.getAttachment('image'); const suggestionImage = interaction.options.getAttachment('image');
const notifEmbed = new client.embed() const notifEmbed = new client.embed()
@ -26,11 +26,10 @@ export default {
suggestionText suggestionText
)); ));
if (suggestionImage) notifEmbed.setImage(suggestionImage.url); if (suggestionImage) notifEmbed.setImage(suggestionImage.url);
webhook.send({embeds: [notifEmbed], username: `${client.user.username} Notification`, avatarURL: client.user.avatarURL({size: 256})} HookMgr.send(client, 'bot_suggestions', '1079621523561779272', {embeds:[notifEmbed], username: `${client.user.username} Suggestions`, avatarURL: client.user.avatarURL({size:256})}).catch(e=>{
).catch(e=>{
console.log(e.message); console.log(e.message);
interaction.reply({content: 'Failed to send suggestion, try again later.', ephemeral: true}) 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'}); 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}) interaction.reply({content: `Suggestion sent, here is your suggestion ID to take note of it: \`${suggestionID}\``, ephemeral: true})
}, },

21
src/funcs/HookManager.ts Normal file
View File

@ -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\`\`\``));
}
}