1
0
mirror of https://github.com/toast-ts/Daggerbot-TS.git synced 2024-09-30 00:50:59 -04:00
Daggerbot-TS/src/funcs/HookManager.ts

25 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-10-06 10:26:23 -04:00
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 {
2023-10-07 04:48:39 -04:00
protected static async channelFetch(client:TClient, channel:ChannelList) {
return await client.channels.fetch(config.mainServer.channels[channel]) as Discord.TextChannel;
}
2023-10-06 10:26:23 -04:00
protected static async fetch(client:TClient, channel:ChannelList, webhookId:Discord.Snowflake) {
2023-10-07 04:48:39 -04:00
const hookInstance = await (await this.channelFetch(client, channel)).fetchWebhooks().then(x=>x.find(y=>y.id===webhookId));
2023-10-06 10:26:23 -04:00
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\`\`\``));
}
}