Skip to content

Commit

Permalink
remove guildid too
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Aug 28, 2024
1 parent b612223 commit f0fafc4
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 84 deletions.
36 changes: 1 addition & 35 deletions src/BaseClient/Bot/DataBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { metricsCollector } from './Metrics.js';
const prisma = new PrismaClient();

prisma.$use(async (params, next) => {
const where = getInterestingWhereClause(params.args);

metricsCollector.dbQuery(params.model ?? '-', params.action, where.guild);
metricsCollector.dbQuery(params.model ?? '-', params.action);

try {
const result = await next(params);
Expand All @@ -21,36 +19,4 @@ prisma.$use(async (params, next) => {
}
});

type Where = { [key: string]: string | unknown[] } & { [key: string]: Where };

const getInterestingWhereClause = (args: Prisma.MiddlewareParams['args']) => {
if (!('where' in args)) return '-';
if (!args.where) return '-';

const where: Where = args.where;

const isArray = (val: Where[string]) => (typeof val !== 'string' ? 'Array' : (val as string));

const values = Object.keys(where).map((k) => {
switch (true) {
case k === 'guildId':
case k === 'guildid':
return { guild: isArray(where.guildid || where.guildId) };
default: {
const val = where[k];

switch (true) {
case k.includes('guildid'):
case k.includes('guildId'):
return { guild: isArray(val.guildid || val.guildId) };
default:
return {};
}
}
}
});

return Object.assign({}, ...values);
};

export default prisma;
14 changes: 3 additions & 11 deletions src/BaseClient/Bot/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,10 @@ export const metricsCollector = {
shardEventsReceived: (clientName: string, opCode: string, shard: number) =>
shardEventsReceived.labels(clientName, opCode, String(shard)).inc(),

dbQuery: (modelName: string, action: string, guild?: string) =>
dbQuery.labels(modelName, action, guild ?? '-').inc(),
dbQuery: (modelName: string, action: string) => dbQuery.labels(modelName, action).inc(),

cmdExecuted: (
command: string,
type: InteractionTypeExtended,
context: 0 | 1,
guild?: string,
) =>
cmdExecuted
.labels(command, getInteractionType(type), context === 0 ? 'Guild' : 'User', guild ?? '-')
.inc(),
cmdExecuted: (command: string, type: InteractionTypeExtended, context: 0 | 1) =>
cmdExecuted.labels(command, getInteractionType(type), context === 0 ? 'Guild' : 'User').inc(),
};

type InteractionTypeExtended = InteractionType | ExtendedTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ export default async (cmd: Discord.Interaction) => {
const command = files.find((f) => f.endsWith(`/AutocompleteCommands/${path()}.js`));
if (!command) return;

metricsCollector.cmdExecuted(
path(),
cmd.type,
cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1,
cmd.guildId ?? undefined,
);
metricsCollector.cmdExecuted(path(), cmd.type, cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1);

const responses = await ((await import(command)) as CT.AutoCompleteFile).default(cmd);

Expand Down
7 changes: 1 addition & 6 deletions src/Events/BotEvents/interactionEvents/buttonHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export default async (cmd: Discord.Interaction) => {
const command = files.find((f) => f.endsWith(`/ButtonCommands/${path}.js`));
if (!command || !path) return;

metricsCollector.cmdExecuted(
path,
cmd.type,
cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1,
cmd.guildId ?? undefined,
);
metricsCollector.cmdExecuted(path, cmd.type, cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1);

(await import(command)).default(cmd, args);
};
7 changes: 1 addition & 6 deletions src/Events/BotEvents/interactionEvents/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,7 @@ export default async (cmd: Discord.Interaction) => {
const command = files.find((f) => f.endsWith(`/SlashCommands/${path()}.js`));
if (!command) return;

metricsCollector.cmdExecuted(
path(),
cmd.type,
cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1,
cmd.guildId || undefined,
);
metricsCollector.cmdExecuted(path(), cmd.type, cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1);

const commandName = command.split(/\//g).pop()?.split(/\./g).shift() as string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ export default async (cmd: Discord.Interaction) => {
const command = files.find((f) => f.endsWith(`/ContextCommands/${path}.js`));
if (!command) return;

metricsCollector.cmdExecuted(
path,
cmd.type,
cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1,
cmd.guildId ?? undefined,
);
metricsCollector.cmdExecuted(path, cmd.type, cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1);

(await import(command)).default(cmd);
};
7 changes: 1 addition & 6 deletions src/Events/BotEvents/interactionEvents/modalHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ export default async (cmd: Discord.Interaction) => {
const command = files.find((f) => f.endsWith(`/ModalCommands/${path}.js`));
if (!command || !path) return;

metricsCollector.cmdExecuted(
path,
cmd.type,
cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1,
cmd.guildId ?? undefined,
);
metricsCollector.cmdExecuted(path, cmd.type, cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1);

(await import(command)).default(cmd, args);
};
7 changes: 1 addition & 6 deletions src/Events/BotEvents/interactionEvents/selectHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ export default async (cmd: Discord.Interaction) => {

if (!command || !path) return;

metricsCollector.cmdExecuted(
path,
cmd.type,
cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1,
cmd.guildId ?? undefined,
);
metricsCollector.cmdExecuted(path, cmd.type, cmd.inCachedGuild() && cmd.inGuild() ? 0 : 1);

(await import(command)).default(cmd, args);
};
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const dmCommand = async (msg: Discord.Message) => {
if (!command) return;
if (!command.dmAllowed) return;

metricsCollector.cmdExecuted(commandName, 0, 1, msg.guildId ?? undefined);
metricsCollector.cmdExecuted(commandName, 0, 1);

const language = await msg.client.util.getLanguage(msg.author.id);
command.default(msg, args, { language, command, prefix });
Expand Down Expand Up @@ -164,7 +164,7 @@ const guildCommand = async (msg: Discord.Message<true>) => {
return;
}

metricsCollector.cmdExecuted(commandName, 0, 0, msg.guildId ?? undefined);
metricsCollector.cmdExecuted(commandName, 0, 0);

command.default(msg, args, { language, command, prefix });
};
Expand Down

0 comments on commit f0fafc4

Please sign in to comment.