Skip to content

Commit

Permalink
Verify log channel
Browse files Browse the repository at this point in the history
  • Loading branch information
Seefaaa committed Sep 24, 2024
1 parent 03524d4 commit 4bcc92d
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ guild_id = ""
[log]
path = "bot.json.log"
colorize = true
verify_channel = ""

[api]
url = "https://api.turkb.us/v2"
Expand Down
43 changes: 39 additions & 4 deletions src/commands/api/verify.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import {
type ChatInputCommandInteraction,
type Client,
PermissionFlagsBits,
SlashCommandBuilder,
type TextChannel,
} from 'discord.js';

import config from '@/config';
import { verifyRegex } from '@/constants';
import logger from '@/logger';
import type { Command } from '@/types';
import { post } from '@/utils';

const logVerify = async (client: Client, message: string) => {
const verifyLogChannel = (await client.channels.fetch(
config.log.verifyChannel
)) as TextChannel;

verifyLogChannel.send({
content: message,
allowedMentions: { parse: [] },
});
};

export class VerifyCommand implements Command {
public builder = new SlashCommandBuilder()
.setName('verify')
Expand All @@ -28,14 +43,19 @@ export class VerifyCommand implements Command {
});

if (statusCode === 200) {
interaction.client.logger.info(
logger.info(
`Verified user [${user.tag}](${user.id}) with ckey \`${ckey}\``
);

interaction.reply({
content: `Discord hesabın \`${ckey}\` adlı BYOND hesabına bağlandı.`,
ephemeral: true,
});

logVerify(
interaction.client,
`@silent ${user} hesabını \`${ckey}\` adlı BYOND hesabına bağladı.`
);
} else if (statusCode === 404) {
if (!verifyRegex.test(code)) {
interaction.reply({
Expand Down Expand Up @@ -107,13 +127,18 @@ export class UnverifyCommand implements Command {
});

if (statusCode === 200) {
interaction.client.logger.info(
logger.info(
`Unverified user [${user.tag}](${user.id}) with ckey \`${ckey}\` by [${interaction.user.tag}](${interaction.user.id})`
);

interaction.reply(
`<@${user.id}> adlı Discord hesabı ile \`${ckey}\` adlı BYOND hesabının bağlantısı kaldırıldı.`
);

logVerify(
interaction.client,
`@silent ${user} adlı Discord hesabı ile \`${ckey}\` adlı BYOND hesabının bağlantısı ${interaction.user} tarafından kaldırıldı.`
);
} else if (statusCode === 409) {
interaction.reply('Hesap zaten bağlı değil.');
}
Expand All @@ -131,13 +156,18 @@ export class UnverifyCommand implements Command {
const userId = discordId.slice(1);
const user = await interaction.client.users.fetch(userId);

interaction.client.logger.info(
logger.info(
`Unverified user [${user.tag}](${userId}) with ckey \`${ckey}\` by [${interaction.user.tag}](${interaction.user.id})`
);

interaction.reply(
`\`${ckey}\` adlı BYOND hesabı ile <${discordId}> adlı Discord hesabının bağlantısı kaldırıldı.`
);

logVerify(
interaction.client,
`@silent ${user} adlı Discord hesabı ile \`${ckey}\` adlı BYOND hesabının bağlantısı ${interaction.user} tarafından kaldırıldı.`
);
} else if (statusCode === 404) {
interaction.reply('Hesap bulunamadı.');
} else if (statusCode === 409) {
Expand Down Expand Up @@ -178,13 +208,18 @@ export class ForceVerifyCommand implements Command {
});

if (statusCode === 200) {
interaction.client.logger.info(
logger.info(
`Force-verified user [${user.tag}](${user.id}) with ckey \`${ckey}\` by [${interaction.user.tag}](${interaction.user.id})`
);

interaction.reply(
`<@${user.id}> adlı Discord hesabı \`${ckey}\` adlı BYOND hesabına bağlandı.`
);

logVerify(
interaction.client,
`@silent ${user} adlı Discord hesabı ile \`${ckey}\` adlı BYOND hesabı ${interaction.user} tarafından bağlandı.`
);
} else if (statusCode === 404) {
interaction.reply('Oyuncu bulunamadı.');
} else if (statusCode === 409) {
Expand Down
18 changes: 15 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ import { TOML } from 'bun';
import type { Config } from '@/types';

if (!existsSync('config.toml')) {
throw new Error('Config: config.toml does not exist in cwd');
throw new Error('Config: config.toml does not exist on cwd');
}

const config = TOML.parse(readFileSync('config.toml', 'utf8')) as Config;

export const botToken = config.bot_token;
export const applicationId = config.application_id;
export const guildId = config.guild_id;
export const log = config.log;
export const log = {
path: config.log?.path,
colorize: config.log?.colorize,
verifyChannel: config.log?.verify_channel,
};
export const api = config.api;

export default {
const default_ = {
botToken,
applicationId,
guildId,
log,
api,
};

export default default_;

// Validate config

if (typeof botToken !== 'string' || botToken.length === 0) {
throw new Error('Config: bot_token is required');
}
Expand All @@ -44,6 +52,10 @@ if (typeof log.colorize !== 'boolean') {
throw new Error('Config: log.colorize is required');
}

if (typeof log.verifyChannel !== 'string' || log.verifyChannel.length === 0) {
throw new Error('Config: log.verify_channel is required');
}

if (typeof api.url !== 'string' || api.url.length === 0) {
throw new Error('Config: api.url is required');
}
Expand Down
13 changes: 6 additions & 7 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Events,
} from 'discord.js';

import logger from '@/logger';
import type { Command, Event } from '@/types';
import { get } from '@/utils';

Expand All @@ -18,7 +19,7 @@ export class InteractionCreateEvent implements Event {
await handleAutocomplete(interaction);
}
} catch (error) {
interaction.client.logger.error(error);
logger.error(error);
}
}
}
Expand All @@ -29,16 +30,14 @@ async function handleChatInputCommand(
const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
interaction.client.logger.error(
`No command matching ${interaction.commandName} was found.`
);
logger.error(`No command matching ${interaction.commandName} was found.`);
return;
}

try {
await command.execute(interaction);
} catch (error) {
interaction.client.logger.error(error);
logger.error(error);
try {
if (interaction.replied || interaction.deferred) {
interaction.followUp({
Expand All @@ -64,7 +63,7 @@ async function handleAutocomplete(interaction: AutocompleteInteraction) {
const command = interaction.client.commands.get(interaction.commandName);

if (!command) {
interaction.client.logger.error(
logger.error(
`No autocomplete matching ${interaction.commandName} was found.`
);
return;
Expand All @@ -81,7 +80,7 @@ async function handleAutocomplete(interaction: AutocompleteInteraction) {

await command.autocomplete!(interaction);
} catch (error) {
interaction.client.logger.error(error);
logger.error(error);
try {
interaction.respond([]);
} catch {}
Expand Down
3 changes: 2 additions & 1 deletion src/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { type Client, Events } from 'discord.js';

import logger from '@/logger';
import type { Event } from '@/types';

export class ReadyEvent implements Event {
public name = Events.ClientReady;
public once = true;
public async execute(client: Client) {
client.logger.info(`Logged in as ${client.user?.tag}!`);
logger.info(`Logged in as ${client.user?.tag}!`);
}
}
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Client, Collection, GatewayIntentBits } from 'discord.js';

import { botToken } from '@/config';
import { createLogger, deployCommands } from '@/utils';
import { deployCommands } from '@/utils';

const client = new Client({
intents: [
Expand All @@ -12,8 +12,6 @@ const client = new Client({
],
});

client.logger = await createLogger();

client.commands = new Collection();

for (const Command of Object.values(await import('./commands'))) {
Expand Down
6 changes: 5 additions & 1 deletion src/utils/logger.ts → src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pretty from 'pino-pretty';
import config from '@/config';
import { name } from '@/package';

export async function createLogger() {
async function createLogger() {
const prettyStream = pretty({
colorize: config.log.colorize,
ignore: 'pid,hostname',
Expand All @@ -24,3 +24,7 @@ export async function createLogger() {

return logger;
}

const logger = await createLogger();

export default logger;
3 changes: 1 addition & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
SlashCommandOptionsOnlyBuilder,
SlashCommandSubcommandsOnlyBuilder,
} from 'discord.js';
import type { Logger } from 'pino';

export interface Command {
builder:
Expand All @@ -30,6 +29,7 @@ export interface Config {
log: {
path: string;
colorize: boolean;
verify_channel: string;
};
api: {
url: string;
Expand All @@ -40,6 +40,5 @@ export interface Config {
declare module 'discord.js' {
export interface Client {
commands: Collection<string, Command>;
logger: Logger;
}
}
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './api';
export * from './deployCommands';
export * from './logger';
export * from './time';

0 comments on commit 4bcc92d

Please sign in to comment.