Skip to content

Commit

Permalink
[PATCH] Updating cmd and cmd perm cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 26, 2023
1 parent c9d6207 commit 524d02e
Show file tree
Hide file tree
Showing 23 changed files with 149 additions and 101 deletions.
5 changes: 3 additions & 2 deletions src/BaseClient/ClientHelperModules/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import webhooks, { Webhooks } from './cache/discord/webhooks.js';
import welcomeScreens, { WelcomeScreens } from './cache/discord/welcomeScreens.js';
import onboarding, { Onboarding } from './cache/discord/onboarding.js';
import invites, { Invites } from './cache/discord/invites.js';
import commands, { Commands } from './cache/discord/commands.js';

import giveawayClaimTimeout, { GiveawayClaimTimeout } from './cache/bot/giveawayClaimTimeout.js';
import mutes, { Mutes } from './cache/bot/mutes.js';
Expand Down Expand Up @@ -58,7 +59,7 @@ const cache: {
stickyTimeouts: StickyTimeouts;
deleteThreads: DeleteThreads;
apis: Map<string, DiscordCore.API>;
commands: Map<string, Discord.ApplicationCommand[]>;
commands: Commands;
punishments: Set<string>;
antispam: Map<string, Discord.Message<true>[]>;
deleteSuggestions: DeleteSuggestions;
Expand Down Expand Up @@ -103,7 +104,7 @@ const cache: {
auditLogs,
deleteThreads,
apis: new Map(),
commands: new Map(),
commands,
punishments: new Set(),
antispam: new Map(),
deleteSuggestions,
Expand Down
59 changes: 59 additions & 0 deletions src/BaseClient/ClientHelperModules/cache/discord/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as Discord from 'discord.js';

/**
* Interface for managing commands for a Discord guild.
*/
export interface Commands {
/**
* Retrieves the commands for a given guild and command ID.
* @param guild The Discord guild to retrieves for.
* @param commandId The ID of the command to retrieves for.
* @returns A Promise that resolves to an array of ApplicationCommands,
* or undefined if no commands are found.
*/
get: (guild: Discord.Guild, commandId: string) => Promise<Discord.ApplicationCommand | undefined>;

/**
* Sets the command s for a given guild and command ID.
* @param guildId The ID of the guild to sets for.
* @param commandId The ID of the command to sets for.
* @param command An array of ApplicationCommands to set.
*/
set: (guildId: string, commandId: string, command: Discord.ApplicationCommand) => void;

/**
* Deletes the commands for a given guild and command ID.
* @param guildId The ID of the guild to deletes for.
* @param commandId The ID of the command to deletes for.
*/
delete: (guildId: string, commandId: string) => void;

/**
* A cache of commands, keyed by guild ID and then by command ID.
*/
cache: Map<string, Map<string, Discord.ApplicationCommand>>;
}

const self: Commands = {
get: async (guild, commandId) => {
const cached = self.cache.get(guild.id)?.get(commandId);
if (cached) return cached;

const requestHandler = (await import('../../requestHandler.js')).request;
const fetched = await requestHandler.commands.getGuildCommands(guild);
if ('message' in fetched) return undefined;

return fetched?.find((f) => f.id === commandId);
},
set: (guildId, commandId, command) => {
if (!self.cache.get(guildId)) self.cache.set(guildId, new Map());
self.cache.get(guildId)?.set(commandId, command);
},
delete: (guildId, commandId) => {
if (self.cache.get(guildId)?.size === 1) self.cache.delete(guildId);
else self.cache.get(guildId)?.delete(commandId);
},
cache: new Map(),
};

export default self;
3 changes: 2 additions & 1 deletion src/BaseClient/ClientHelperModules/getCustomCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async (guild: Discord.Guild | undefined | null, name: CommandName
client.application?.commands.cache.find((c) => c.name === name);

return guild
? cache.commands.get(guild.id)?.find((c) => c.name === name) ?? clientCommand
? [...(cache.commands.cache.get(guild.id)?.values() ?? [])]?.find((c) => c.name === name) ??
clientCommand
: clientCommand;
};
5 changes: 4 additions & 1 deletion src/BaseClient/ClientHelperModules/helpHelpers/getEmbeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export default (
) => {
const lan = language.slashCommands.help;
const fetchedCommands = cmd.guildId
? cache.commands.get(cmd.guildId) ?? cmd.client.application.commands.cache.map((c) => c)
? [
...(cache.commands.cache.get(cmd.guildId)?.values() ??
cmd.client.application.commands.cache.map((c) => c)),
]
: cmd.client.application.commands.cache.map((c) => c);

const embed: Discord.APIEmbed = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ export default async (guild: Discord.Guild, body: Discord.RESTPutAPIApplicationC
.bulkOverwriteGlobalCommands(await getBotIdFromGuild(guild), body)
.then((cmds) => {
const parsed = cmds.map((cmd) => new Classes.ApplicationCommand(guild.client, cmd));
if (cache.apis.get(guild.id)) {
cache.commands.set(guild.id, parsed);
return parsed;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
parsed.forEach((p) => {
cache.commands.cache.get(guild.id)?.set(p.id, p);

if (cache.apis.get(guild.id)) return;
guild.commands.cache.set(p.id, p);
});

parsed.forEach((p) => guild.client.application.commands.cache.set(p.id, p));
return parsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export default async (
const parsed = cmds.map(
(cmd) => new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id),
);
if (cache.apis.get(guild.id)) {
cache.commands.set(guild.id, parsed);
return parsed;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
parsed.forEach((p) => {
cache.commands.cache.get(guild.id)?.set(p.id, p);

if (cache.apis.get(guild.id)) return;
guild.commands.cache.set(p.id, p);
});

parsed.forEach((p) => guild.commands.cache.set(p.id, p));
return parsed;
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@ export default async (guild: Discord.Guild, body: Discord.RESTPostAPIApplication
.createGlobalCommand(await getBotIdFromGuild(guild), body)
.then((cmd) => {
const parsed = new Classes.ApplicationCommand(guild.client, cmd);
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
cache.commands.cache.get(guild.id)?.set(parsed.id, parsed);

if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed));

return parsed;
}

if (guild.client.application.commands.cache.get(parsed.id)) return parsed;
if (cache.apis.get(guild.id)) return parsed;
guild.client.application.commands.cache.set(parsed.id, parsed);
return parsed;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ export default async (
.createGuildCommand(await getBotIdFromGuild(guild), guild.id, body)
.then((cmd) => {
const parsed = new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id);
if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed));

return parsed;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
cache.commands.cache.get(guild.id)?.set(parsed.id, parsed);

if (cache.apis.get(guild.id)) return parsed;
guild.commands.cache.set(cmd.id, parsed);
return parsed;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@ export default async (guild: Discord.Guild, commandId: string) =>
(cache.apis.get(guild.id) ?? API).applicationCommands
.deleteGlobalCommand(await getBotIdFromGuild(guild), commandId)
.then(() => {
if (cache.apis.get(guild.id)) {
cache.commands.set(
guild.id,
cache.commands.get(guild.id)!.filter((c) => c.id !== commandId),
);

if (cache.commands.get(guild.id)!.length > 0) return;
cache.commands.delete(guild.id);
return;
}
cache.commands.delete(guild.id, commandId);

if (cache.apis.get(guild.id)) return;
guild.client.application.commands.cache.delete(commandId);
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@ export default async (guild: Discord.Guild, commandId: string) =>
(cache.apis.get(guild.id) ?? API).applicationCommands
.deleteGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId)
.then(() => {
if (cache.apis.get(guild.id)) {
cache.commands.set(
guild.id,
cache.commands.get(guild.id)!.filter((c) => c.id !== commandId),
);

if (cache.commands.get(guild.id)!.length > 0) return;
cache.commands.delete(guild.id);
return;
}
cache.commands.delete(guild.id, commandId);

if (cache.apis.get(guild.id)) return;
guild.commands.cache.delete(commandId);
})
.catch((e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ export default async (
.editGlobalCommand(await getBotIdFromGuild(guild), commandId, body)
.then((cmd) => {
const parsed = new Classes.ApplicationCommand(guild.client, cmd);
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
cache.commands.cache.get(guild.id)?.set(parsed.id, parsed);

if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed));

return parsed;
}

if (cache.apis.get(guild.id)) return parsed;
if (guild.client.application.commands.cache.get(parsed.id)) return parsed;
guild.client.application.commands.cache.set(parsed.id, parsed);
return parsed;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Discord from 'discord.js';
import { API } from '../../../Client.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';
import cache from '../../cache.js';
import * as Classes from '../../../Other/classes.js';
import cache from '../../cache.js';
import error from '../../error.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';

/**
* Edits a guild command for a given guild.
Expand All @@ -21,20 +21,10 @@ export default async (
.editGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId, body)
.then((cmd) => {
const parsed = new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id);
if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]);
else {
cache.commands.set(
guild.id,
cache.commands
.get(guild.id)!
.filter((c) => c.id !== parsed.id)!
.concat(parsed),
);
}
return parsed;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
cache.commands.cache.get(guild.id)?.set(parsed.id, parsed);

if (cache.apis.get(guild.id)) return parsed;
guild.commands.cache.set(parsed.id, parsed);
return parsed;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default async (
) =>
(cache.apis.get(guild.id) ?? API).applicationCommands
.editGuildCommandPermissions(userToken, await getBotIdFromGuild(guild), guild.id, commandId, body)
.then((res) => {
cache.commandPermissions.set(guild.id, commandId, res.permissions);
return res.permissions;
})
.catch((e) => {
error(guild, new Error((e as Discord.DiscordAPIError).message));
return e as Discord.DiscordAPIError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ import error from '../../error.js';
*/
export default async (guild: Discord.Guild, commandId: string) =>
guild.client.application.commands.cache.get(commandId) ??
cache.commands.cache.get(guild.id)?.get(commandId) ??
(cache.apis.get(guild.id) ?? API).applicationCommands
.getGlobalCommand(await getBotIdFromGuild(guild), commandId)
.then((cmd) => {
const parsed = new Classes.ApplicationCommand(guild.client, cmd);
if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed));

return parsed;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
cache.commands.cache.get(guild.id)?.set(parsed.id, parsed);

if (cache.apis.get(guild.id)) return parsed;
if (guild.client.application.commands.cache.get(parsed.id)) return parsed;
guild.client.application.commands.cache.set(parsed.id, parsed);
return parsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ export default async (guild: Discord.Guild, query?: Discord.RESTGetAPIApplicatio
.getGlobalCommands(await getBotIdFromGuild(guild), query)
.then((cmds) => {
const parsed = cmds.map((cmd) => new Classes.ApplicationCommand(guild.client, cmd));
parsed.forEach((p) => {
if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [p]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(p));

return;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
parsed.forEach((p) => {
cache.commands.cache.get(guild.id)?.set(p.id, p);

if (cache.apis.get(guild.id)) return;
if (guild.client.application.commands.cache.get(p.id)) return;
guild.client.application.commands.cache.set(p.id, p);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ export default async (guild: Discord.Guild, commandId: string) =>
.getGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId)
.then((cmd) => {
const parsed = new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id);
if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [parsed]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(parsed));

return parsed;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
cache.commands.cache.get(guild.id)?.set(parsed.id, parsed);

if (cache.apis.get(guild.id)) return parsed;
guild.commands.cache.set(cmd.id, parsed);
return parsed;
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as Discord from 'discord.js';
import { API } from '../../../Client.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';
import cache from '../../cache.js';
import error from '../../error.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';

/**
* Retrieves the permissions for a specific command in a guild.
Expand All @@ -13,6 +13,10 @@ import error from '../../error.js';
export default async (guild: Discord.Guild, commandId: string) =>
(cache.apis.get(guild.id) ?? API).applicationCommands
.getGuildCommandPermissions(await getBotIdFromGuild(guild), guild.id, commandId)
.then((res) => {
cache.commandPermissions.set(guild.id, commandId, res.permissions);
return res.permissions;
})
.catch((e) => {
error(guild, new Error((e as Discord.DiscordAPIError).message));
return e as Discord.DiscordAPIError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ export default async (
const parsed = cmds.map(
(cmd) => new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id),
);
parsed.forEach((p) => {
if (cache.apis.get(guild.id)) {
if (!cache.commands.get(guild.id)) cache.commands.set(guild.id, [p]);
else cache.commands.set(guild.id, cache.commands.get(guild.id)!.concat(p));

return;
}
if (!cache.commands.cache.get(guild.id)) cache.commands.cache.set(guild.id, new Map());
parsed.forEach((p) => {
cache.commands.cache.get(guild.id)?.set(p.id, p);

if (cache.apis.get(guild.id)) return;
if (guild.commands.cache.get(p.id)) return;
guild.commands.cache.set(p.id, p);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ import error from '../../error.js';
export default async (guild: Discord.Guild) =>
(cache.apis.get(guild.id) ?? API).applicationCommands
.getGuildCommandsPermissions(await getBotIdFromGuild(guild), guild.id)
.then((res) => {
res.forEach((r) => {
cache.commandPermissions.set(guild.id, r.id, r.permissions);
return r.permissions;
});

return res;
})
.catch((e) => {
error(guild, new Error((e as Discord.DiscordAPIError).message));
return e as Discord.DiscordAPIError;
Expand Down
Loading

0 comments on commit 524d02e

Please sign in to comment.