Skip to content

Commit

Permalink
cache custom clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Aug 28, 2024
1 parent 072cd96 commit 8ff8c5f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/BaseClient/UtilModules/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const cache: {
gifs: typeof gifs;
latelySavedUsers: Map<string, number>;
hasFetchedAllMembers: Set<string>;
customClients: Map<string, string>;

// URLs
urlTLDs: UrlTLDs;
Expand Down Expand Up @@ -104,6 +105,7 @@ const cache: {
commandPermissions,
interactedGuilds: new Set(),
interactedChannels: new Set(),
customClients: new Map(),

// Cache
giveawayClaimTimeout,
Expand Down
14 changes: 11 additions & 3 deletions src/BaseClient/UtilModules/getBotIdFrom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Discord from 'discord.js';
import DataBase from '../Bot/DataBase.js';
import cache from './cache.js';

/**
* Extracts the bot ID from a Discord bot token.
Expand All @@ -20,12 +21,19 @@ export const guild = async (g: Discord.Guild) => {
return client.user!.id;
}

if (cache.customClients.get(g.id)) return cache.customClients.get(g.id)!;

const settings = await DataBase.customclients.findUnique({
where: { guildid: g.id, token: { not: null } },
select: { token: true },
});
if (!settings) return g.client.user.id;
if (!settings.token) return g.client.user.id;
if (!settings || !settings.token) {
cache.customClients.set(g.id, g.client.user.id);
return g.client.user.id;
}

const id = token(settings.token);
cache.customClients.set(g.id, id);

return token(settings.token) ?? g.client.user.id;
return id;
};
3 changes: 2 additions & 1 deletion src/Commands/SlashCommands/settings/custom-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const postChange: CT.SettingsFile<typeof name>['postChange'] = async (
};

const tokenDelete = (guild: Discord.Guild) => {
client.util.cache.customClients.delete(guild.id);
client.util.cache.apis.delete(guild.id);
client.util.DataBase.customclients
.update({
Expand All @@ -161,9 +162,9 @@ const tokenCreate = async (
updateApp(guild);
if (!meIsValid(guild, me)) return;


sendWebhookRequest(guild, me.id);
doCommands(guild, me);
guild.client.util.cache.customClients.delete(guild.id);
};

const updateApp = (guild: Discord.Guild) => {
Expand Down
1 change: 1 addition & 0 deletions src/Events/BotEvents/guildEvents/guildDelete/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export default (guild: Discord.Guild) => {
cache.deleteSuggestions.cache.delete(guild.id);
cache.vcDeleteTimeout.cache.get(guild.id)?.forEach((i) => i?.cancel());
cache.vcDeleteTimeout.cache.delete(guild.id);
cache.customClients.delete(guild.id);

const { DataBase } = guild.client.util;
DataBase.guilds.delete({ where: { guildid: guild.id } }).then();
Expand Down

0 comments on commit 8ff8c5f

Please sign in to comment.