Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { container } from '@sapphire/framework';
- import { isNullish } from '@sapphire/utilities';
- import { MessagePayload, TextChannel, Webhook, WebhookCreateMessageOptions } from 'discord.js';
- export type BeholderHook = Webhook | null;
- export type BeholderHookOptions = string | MessagePayload | Omit<WebhookCreateMessageOptions, 'flags'>;
- export const WebhookAvatarUrlLookup = {
- shop: 'https://cdn.discordapp.com/attachments/984143582724259923/1012414471383306321/unknown.png'
- };
- export class WebhookManager {
- #logger = container.client.logger;
- #maintenanceChannelId: string = container._config.configChannel;
- beholderHook: Webhook;
- public async initializeWebhooks() {
- try {
- let serverConfigurationChannel = container.client.guilds.cache
- .get(container._config.guildId)
- .channels.cache.get(this.#maintenanceChannelId) as TextChannel;
- if (isNullish(serverConfigurationChannel)) {
- serverConfigurationChannel = (await container.client.channels.fetch(container._config.configChannel)) as TextChannel;
- }
- const webhooks = await serverConfigurationChannel.fetchWebhooks();
- let createWebhook = true;
- if (webhooks.size >= 1) {
- for (const wh of webhooks) {
- if (wh[1].applicationId === container.client.user.id) {
- createWebhook = false;
- this.beholderHook = wh[1];
- break;
- }
- }
- }
- if (createWebhook) {
- await serverConfigurationChannel
- .createWebhook({
- avatar: container.client.user.avatarURL(),
- name: 'BeholderHook'
- })
- .then((hook) => {
- this.beholderHook = hook;
- });
- }
- await this.sendMaintenceMessage(`Beholder Webhook initialized`);
- this.#logger.info(`Beholder Hook registered.\nWebhook Data:\nChannel ID: ${this.beholderHook.channelId}\nGuildId: ${this.beholderHook.guildId}`);
- } catch (error) {
- this.#logger.error(error);
- }
- }
- public async sendMaintenceMessage(content: string) {
- const { configChannel } = container._config;
- if (this.beholderHook.channelId !== configChannel) await this.beholderHook.edit({ channel: configChannel });
- return this.beholderHook.send({ content, avatarURL: container.client.user?.avatarURL() });
- }
- public async sendShopkeeperMessage(options: WebhookCreateMessageOptions, channel: string | TextChannel, npcName: string) {
- if (this.beholderHook.channelId !== (typeof channel === 'string' ? channel : channel.id))
- await this.beholderHook.edit({ channel: typeof channel === 'string' ? channel : channel.id });
- return this.beholderHook.send({
- username: `${npcName} [NPC]`,
- avatarURL: WebhookAvatarUrlLookup.shop,
- ...options
- });
- }
- public async sendNpcMessage(options: WebhookCreateMessageOptions, channel: string, npcName: string, avatarURL: string) {
- if (this.beholderHook.channelId !== channel) await this.beholderHook.edit({ channel: channel });
- return this.beholderHook.send({
- username: `${npcName} [NPC]`,
- avatarURL,
- ...options
- });
- }
- public async sendTownCrierMessage(options: WebhookCreateMessageOptions, channel: string) {
- if (this.beholderHook.channelId !== channel) await this.beholderHook.edit({ channel: channel });
- return this.beholderHook.send({
- username: `Town Crier [NPC]`,
- avatarURL: 'https://cdn.discordapp.com/attachments/974836668735578143/978027942905008168/unknown.png',
- ...options
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment