WhacKSr

src/lib/classes/incoming/webhookManager.ts

Jan 16th, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { container } from '@sapphire/framework';
  2. import { isNullish } from '@sapphire/utilities';
  3. import { MessagePayload, TextChannel, Webhook, WebhookCreateMessageOptions } from 'discord.js';
  4.  
  5. export type BeholderHook = Webhook | null;
  6. export type BeholderHookOptions = string | MessagePayload | Omit<WebhookCreateMessageOptions, 'flags'>;
  7. export const WebhookAvatarUrlLookup = {
  8.     shop: 'https://cdn.discordapp.com/attachments/984143582724259923/1012414471383306321/unknown.png'
  9. };
  10. export class WebhookManager {
  11.     #logger = container.client.logger;
  12.     #maintenanceChannelId: string = container._config.configChannel;
  13.     beholderHook: Webhook;
  14.  
  15.     public async initializeWebhooks() {
  16.         try {
  17.             let serverConfigurationChannel = container.client.guilds.cache
  18.                 .get(container._config.guildId)
  19.                 .channels.cache.get(this.#maintenanceChannelId) as TextChannel;
  20.             if (isNullish(serverConfigurationChannel)) {
  21.                 serverConfigurationChannel = (await container.client.channels.fetch(container._config.configChannel)) as TextChannel;
  22.             }
  23.             const webhooks = await serverConfigurationChannel.fetchWebhooks();
  24.             let createWebhook = true;
  25.             if (webhooks.size >= 1) {
  26.                 for (const wh of webhooks) {
  27.                     if (wh[1].applicationId === container.client.user.id) {
  28.                         createWebhook = false;
  29.                         this.beholderHook = wh[1];
  30.                         break;
  31.                     }
  32.                 }
  33.             }
  34.             if (createWebhook) {
  35.                 await serverConfigurationChannel
  36.                     .createWebhook({
  37.                         avatar: container.client.user.avatarURL(),
  38.                         name: 'BeholderHook'
  39.                     })
  40.                     .then((hook) => {
  41.                         this.beholderHook = hook;
  42.                     });
  43.             }
  44.  
  45.             await this.sendMaintenceMessage(`Beholder Webhook initialized`);
  46.             this.#logger.info(`Beholder Hook registered.\nWebhook Data:\nChannel ID: ${this.beholderHook.channelId}\nGuildId: ${this.beholderHook.guildId}`);
  47.         } catch (error) {
  48.             this.#logger.error(error);
  49.         }
  50.     }
  51.  
  52.     public async sendMaintenceMessage(content: string) {
  53.         const { configChannel } = container._config;
  54.         if (this.beholderHook.channelId !== configChannel) await this.beholderHook.edit({ channel: configChannel });
  55.         return this.beholderHook.send({ content, avatarURL: container.client.user?.avatarURL() });
  56.     }
  57.  
  58.     public async sendShopkeeperMessage(options: WebhookCreateMessageOptions, channel: string | TextChannel, npcName: string) {
  59.         if (this.beholderHook.channelId !== (typeof channel === 'string' ? channel : channel.id))
  60.             await this.beholderHook.edit({ channel: typeof channel === 'string' ? channel : channel.id });
  61.         return this.beholderHook.send({
  62.             username: `${npcName} [NPC]`,
  63.             avatarURL: WebhookAvatarUrlLookup.shop,
  64.             ...options
  65.         });
  66.     }
  67.  
  68.     public async sendNpcMessage(options: WebhookCreateMessageOptions, channel: string, npcName: string, avatarURL: string) {
  69.         if (this.beholderHook.channelId !== channel) await this.beholderHook.edit({ channel: channel });
  70.  
  71.         return this.beholderHook.send({
  72.             username: `${npcName} [NPC]`,
  73.             avatarURL,
  74.             ...options
  75.         });
  76.     }
  77.  
  78.     public async sendTownCrierMessage(options: WebhookCreateMessageOptions, channel: string) {
  79.         if (this.beholderHook.channelId !== channel) await this.beholderHook.edit({ channel: channel });
  80.  
  81.         return this.beholderHook.send({
  82.             username: `Town Crier [NPC]`,
  83.             avatarURL: 'https://cdn.discordapp.com/attachments/974836668735578143/978027942905008168/unknown.png',
  84.             ...options
  85.         });
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment