First iteration!
This commit is contained in:
4
src/lib/constants.ts
Normal file
4
src/lib/constants.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { join } from 'path';
|
||||
|
||||
export const rootDir = join(__dirname, '..', '..');
|
||||
export const srcDir = join(rootDir, 'src');
|
18
src/lib/setup.ts
Normal file
18
src/lib/setup.ts
Normal file
@ -0,0 +1,18 @@
|
||||
// Unless explicitly defined, set NODE_ENV as development:
|
||||
process.env.NODE_ENV ??= 'development';
|
||||
|
||||
import { ApplicationCommandRegistries, RegisterBehavior } from '@sapphire/framework';
|
||||
import '@sapphire/plugin-logger/register';
|
||||
import { setup } from '@skyra/env-utilities';
|
||||
import * as colorette from 'colorette';
|
||||
import { join } from 'node:path';
|
||||
import { srcDir } from './constants';
|
||||
|
||||
// Set default behavior to bulk overwrite
|
||||
ApplicationCommandRegistries.setDefaultBehaviorWhenNotIdentical(RegisterBehavior.BulkOverwrite);
|
||||
|
||||
// Read env var
|
||||
setup({ path: join(srcDir, '.env') });
|
||||
|
||||
// Enable colorette
|
||||
colorette.createColors({ useColor: true });
|
47
src/lib/utils.ts
Normal file
47
src/lib/utils.ts
Normal file
@ -0,0 +1,47 @@
|
||||
import {
|
||||
container,
|
||||
type ChatInputCommandSuccessPayload,
|
||||
type Command,
|
||||
type ContextMenuCommandSuccessPayload,
|
||||
type MessageCommandSuccessPayload
|
||||
} from '@sapphire/framework';
|
||||
import { cyan } from 'colorette';
|
||||
import type { APIUser, Guild, User } from 'discord.js';
|
||||
|
||||
export function logSuccessCommand(payload: ContextMenuCommandSuccessPayload | ChatInputCommandSuccessPayload | MessageCommandSuccessPayload): void {
|
||||
let successLoggerData: ReturnType<typeof getSuccessLoggerData>;
|
||||
|
||||
if ('interaction' in payload) {
|
||||
successLoggerData = getSuccessLoggerData(payload.interaction.guild, payload.interaction.user, payload.command);
|
||||
} else {
|
||||
successLoggerData = getSuccessLoggerData(payload.message.guild, payload.message.author, payload.command);
|
||||
}
|
||||
|
||||
container.logger.debug(`${successLoggerData.shard} - ${successLoggerData.commandName} ${successLoggerData.author} ${successLoggerData.sentAt}`);
|
||||
}
|
||||
|
||||
export function getSuccessLoggerData(guild: Guild | null, user: User, command: Command) {
|
||||
const shard = getShardInfo(guild?.shardId ?? 0);
|
||||
const commandName = getCommandInfo(command);
|
||||
const author = getAuthorInfo(user);
|
||||
const sentAt = getGuildInfo(guild);
|
||||
|
||||
return { shard, commandName, author, sentAt };
|
||||
}
|
||||
|
||||
function getShardInfo(id: number) {
|
||||
return `[${cyan(id.toString())}]`;
|
||||
}
|
||||
|
||||
function getCommandInfo(command: Command) {
|
||||
return cyan(command.name);
|
||||
}
|
||||
|
||||
function getAuthorInfo(author: User | APIUser) {
|
||||
return `${author.username}[${cyan(author.id)}]`;
|
||||
}
|
||||
|
||||
function getGuildInfo(guild: Guild | null) {
|
||||
if (guild === null) return 'Direct Messages';
|
||||
return `${guild.name}[${cyan(guild.id)}]`;
|
||||
}
|
Reference in New Issue
Block a user