mirror of
https://github.com/toast-ts/Daggerbot-TS.git
synced 2024-11-17 12:21:00 -05:00
Create HookManager.ts
This commit is contained in:
parent
6b676ad5e6
commit
2767eca018
@ -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<Discord.TextChannel>)).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})
|
||||
},
|
||||
|
21
src/funcs/HookManager.ts
Normal file
21
src/funcs/HookManager.ts
Normal 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\`\`\``));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user