Skip to content

Commit

Permalink
fix(network): add webhook caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Dec 3, 2023
1 parent 60e439f commit e0269b1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/SuperClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Collection,
Snowflake,
Guild,
WebhookClient,
} from 'discord.js';
import { ClusterClient, getInfo } from 'discord-hybrid-sharding';
import { commandsMap, interactionsMap } from './commands/BaseCommand.js';
Expand All @@ -30,6 +31,7 @@ export default abstract class SuperClient extends Client {
readonly version = process.env.npm_package_version ?? 'Unknown';
readonly commands = commandsMap;
readonly interactions = interactionsMap;
readonly webhooks = new Collection<string, WebhookClient>;

readonly commandCooldowns = new CooldownService();
readonly reactionCooldowns = new Collection<string, number>();
Expand Down Expand Up @@ -76,7 +78,7 @@ export default abstract class SuperClient extends Client {
status: 'idle',
activities: [
{
state: 'Watching over 400+ networks | /hub browse',
state: 'Watching over 500+ networks | /hub browse',
name: 'custom',
type: ActivityType.Custom,
},
Expand Down
8 changes: 6 additions & 2 deletions src/managers/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ export default class NetworkManager extends Factory {
try {
// parse the webhook url and get the webhook id and token
// fetch the webhook from discord
const webhook = new WebhookClient({ url: connection.webhookURL });
let webhook = message.client.webhooks.get(connection.webhookURL);

if (!webhook) {
webhook = new WebhookClient({ url: connection.webhookURL });
message.client.webhooks.set(connection.webhookURL, webhook);
}

const reply = referenceInDb?.channelAndMessageIds.find(
(msg) => msg.channelId === connection.channelId,
Expand Down Expand Up @@ -554,7 +559,6 @@ export default class NetworkManager extends Factory {
authorId: message.author.id,
serverId: message.guild.id,
referenceDocId: dbReference?.id,
reactions: {},
},
});
}
Expand Down
1 change: 1 addition & 0 deletions src/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ declare module 'discord.js' {
readonly commandCooldowns: CooldownService;
readonly reactionCooldowns: Collection<string, number>;
readonly cluster: ClusterClient<Client>;
readonly webhooks: Collection<string, WebhookClient>;

resolveEval: <T>(value: T[]) => T | undefined;

Expand Down

0 comments on commit e0269b1

Please sign in to comment.