Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
dist
.env
node_modules/
dist/
.env

.idea/
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
40 changes: 40 additions & 0 deletions resources/guildSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"1325571365079879774": {
"channels": {
"logging": "1442896999237030031",
"commits": "1489996651551522816",
"errors": "1491479994994659428"
},
"roles": {
"updates": "1445497401614925914",
"qotd": "1445497592573067546",
"support": "1445887485853962300",
"community": "1416251642185121823"
},
"trusted": [
"1441859003708866601",
"768481984242253904",
"855798460593733652",
"1382022366040686763"
]
},
"1310540137096282122": {
"channels": {
"logging": "1492607126638563459",
"commits": "1492607196301758525",
"errors": "1492607206346981557"
},
"roles": {
"updates": "1492607385112674354",
"qotd": "1492607410483761232",
"support": "1492607474996346952",
"community": "1492607539919978646"
},
"trusted": [
"1441859003708866601",
"768481984242253904",
"855798460593733652",
"1382022366040686763"
]
}
}
13 changes: 11 additions & 2 deletions src/commands/admin/DevResetCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PermissionsBitField } from 'discord.js';
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
import { ExtendedClient } from '@structures/Client.js';
import { Embeds } from '@utils/Embeds.js';
import { Constants } from '@utils/Constants.js';
import * as ds from '@data/DataStore.js';

export default class DevResetCommand extends Command {
constructor() {
Expand All @@ -16,8 +16,17 @@ export default class DevResetCommand extends Command {

public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
const author = context.author!;
const data = ds.getDataFromContext(context);

if (!Constants.TRUSTED_USER_IDS.includes(author.id)) {
if (!data) {
await context.reply({
embeds: [Embeds.error('Something went wrong, please try again later.')],
});

return;
}

if (!data.trusted.includes(author.id)) {
await context.reply({
embeds: [Embeds.error('You are not authorized to use this command.')],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@ import { PermissionsBitField } from 'discord.js';
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
import { ExtendedClient } from '@structures/Client.js';
import { Embeds } from '@utils/Embeds.js';
import { Constants } from '@utils/Constants.js';
import * as ds from '@data/DataStore.js';

export default class CrashCommand extends Command {
export default class ShutdownCommand extends Command {
constructor() {
super({
name: 'crash',
description: 'Crash the bot',
name: 'shutdown',
description: 'Shut down the bot',
requiredPermissions: [PermissionsBitField.Flags.Administrator],
checkFlags: CommandCheckFlags.Author | CommandCheckFlags.Guild,
});
}

public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
const author = context.author!;
const data = ds.getDataFromContext(context);

if (!Constants.TRUSTED_USER_IDS.includes(author.id)) {
if (!data) {
return await context.reply({
embeds: [Embeds.error('Something went wrong, please try again later.')],
});
}

if (!data.trusted.includes(author.id)) {
await context.reply({
embeds: [Embeds.error('You are not authorized to use this command.')],
});
Expand All @@ -26,9 +33,10 @@ export default class CrashCommand extends Command {
}

await context.reply({
embeds: [Embeds.error('Crashing...')],
embeds: [Embeds.error('Shutting down...')],
});

process.exit(0);
await client.destroy();
process.exit();
}
}
17 changes: 12 additions & 5 deletions src/commands/admin/SyncCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PermissionFlagsBits } from 'discord.js';
import { ExtendedClient } from '@structures/Client.js';
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
import { Embeds } from '@utils/Embeds.js';
import { Constants } from '@utils/Constants.js';
import * as ds from '@data/DataStore.js';

export default class SyncCommand extends Command {
constructor() {
Expand All @@ -16,16 +16,23 @@ export default class SyncCommand extends Command {

public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
const guild = context.guild!;
const data = ds.getDataFromContext(context);

if (!data) {
return await context.reply({
embeds: [Embeds.error('Something went wrong, please try again later.')],
});
}

const members = await guild.members.fetch();
const communityRole = guild.roles.cache.get(Constants.ROLES.COMMUNITY);
const communityRole = guild.roles.cache.get(data.roles.community);
const ignoredRoles = new Set(
[
guild.id, // @everyone role
guild.roles.premiumSubscriberRole?.id,
guild.roles.cache.get(Constants.ROLES.UPDATES)?.id,
guild.roles.cache.get(Constants.ROLES.QOTD_PING)?.id,
guild.roles.cache.get(Constants.ROLES.SUPPORT)?.id,
guild.roles.cache.get(data.roles.updates)?.id,
guild.roles.cache.get(data.roles.qotd)?.id,
guild.roles.cache.get(data.roles.support)?.id,
].filter((id) => id != null)
);

Expand Down
13 changes: 11 additions & 2 deletions src/commands/admin/UpdateGeminiKeyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ExtendedClient } from '@structures/Client.js';
import { Command, CommandContext, CommandCheckFlags } from '@structures/Command.js';
import { Argument } from '@structures/Argument.js';
import { Embeds } from '@utils/Embeds.js';
import { Constants } from '@utils/Constants.js';
import * as ds from '@data/DataStore.js';

export default class UpdateGeminiKeyCommand extends Command {
constructor() {
Expand All @@ -25,8 +25,17 @@ export default class UpdateGeminiKeyCommand extends Command {

public async execute(client: ExtendedClient, context: CommandContext): Promise<void> {
const author = context.author!;
const data = ds.getDataFromContext(context);

if (!Constants.TRUSTED_USER_IDS.includes(author.id)) {
if (!data) {
await context.reply({
embeds: [Embeds.error('Something went wrong, please try again later.')],
});

return;
}

if (!data.trusted.includes(author.id)) {
await context.reply({
embeds: [Embeds.error('You are not authorized to use this command.')],
});
Expand Down
2 changes: 2 additions & 0 deletions src/data/Config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const GUILD_SETTINGS_FILE = 'resources/guildSettings.json';
export const COBALT_GUILD_ID = '1325571365079879774';
82 changes: 82 additions & 0 deletions src/data/DataStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
type ChannelTypes = 'logging' | 'commits' | 'errors';
type RoleTypes = 'updates' | 'qotd' | 'support' | 'community';
import * as fs from 'fs';
import { GUILD_SETTINGS_FILE } from '@data/Config.js';
import { CommandContext } from '@structures/Command.js';
import { validateGuildSettingsFile } from '../utils/JsonValidation.js';
import { Logger } from '@utils/Logger.js';
import { IllegalArgumentError } from '../errors/IllegalArgumentError.js';

export class GuildData {
readonly guildId: string;
channels: Record<ChannelTypes, string>;
roles: Record<RoleTypes, string>;
trusted: string[];

constructor(guildId: string) {
this.guildId = guildId;
this.channels = {
logging: '',
commits: '',
errors: '',
};
this.roles = {
updates: '',
qotd: '',
support: '',
community: '',
};
this.trusted = [];
}

toJson(): string {
return JSON.stringify(
{
[this.guildId]: {
channels: this.channels,
roles: this.roles,
trusted: this.trusted,
},
},
null,
2
);
}
}

export let dataStore: GuildData[] = loadFromJson(GUILD_SETTINGS_FILE);

function loadFromJson(jsonFilePath: string): GuildData[] {
const fileContent = fs.readFileSync(jsonFilePath, 'utf-8');
const data = JSON.parse(fileContent) as unknown;
validateGuildSettingsFile(data, jsonFilePath);

const values: GuildData[] = [];
for (const guildId of Object.keys(data)) {
const guildObj = data[guildId];
const guildData = new GuildData(guildId);
guildData.channels = guildObj.channels;
guildData.roles = guildObj.roles;
guildData.trusted = guildObj.trusted;
values.push(guildData);
}
return values;
}

export function getDataFromContext(context: CommandContext): GuildData | undefined {
if (!context.guild) {
return undefined;
}
return getDataForGuild(context.guild.id);
}

export function getDataForGuild(guildId: string): GuildData {
const data = dataStore.find((guild) => guild.guildId === guildId);
if (data === undefined) {
throw new IllegalArgumentError(
`Requested data for guild: ${guildId}, but this guild is not found in the guild settings file!`
);
}

return data;
}
6 changes: 6 additions & 0 deletions src/errors/IllegalArgumentError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class IllegalArgumentError extends Error {
constructor(message: string) {
super(message);
this.name = 'IllegalArgumentError';
}
}
6 changes: 6 additions & 0 deletions src/errors/RuntimeError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class RuntimeError extends Error {
constructor(message: string) {
super(message);
this.name = 'RuntimeException';
}
}
11 changes: 10 additions & 1 deletion src/events/ChatBotHandleEvent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Event } from '@structures/Event.js';
import { ExtendedClient } from '@structures/Client.js';
import { Message, PermissionFlagsBits } from 'discord.js';
import { isErrorWithMessage, messageOrJsonToMessage } from '@utils/ErrorUtil.js';
import { Logger } from '@utils/Logger.js';

export default class ChatBotHandleEvent extends Event<'messageCreate'> {
constructor() {
Expand Down Expand Up @@ -32,7 +34,14 @@ export default class ChatBotHandleEvent extends Event<'messageCreate'> {

await message.channel.sendTyping();

const res = await client.chatBot.generateResponse(content, message.author);
let res;
try {
res = await client.chatBot.generateResponse(content, message.author);
} catch (error) {
if (isErrorWithMessage(error)) {
await Logger.logErrorWithBot(messageOrJsonToMessage(error.message), message.guild);
}
}

if (!res || res.length === 0) {
return;
Expand Down
3 changes: 1 addition & 2 deletions src/events/ClientReadyEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Logger } from '@utils/Logger.js';
import { CommandManager } from '@structures/CommandManager.js';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Constants } from '@utils/Constants.js';

export default class ClientReadyEvent extends Event<'clientReady'> {
constructor() {
Expand All @@ -18,7 +17,7 @@ export default class ClientReadyEvent extends Event<'clientReady'> {
Logger.success(`Logged in as ${client.user?.tag}`);

const commandsDirectory = join(dirname(fileURLToPath(import.meta.url)), '..', 'commands');
const commandManager = new CommandManager(client, Constants.GUILD_ID);
const commandManager = new CommandManager(client);

await commandManager.loadCommands(commandsDirectory);

Expand Down
2 changes: 1 addition & 1 deletion src/events/InteractionCreateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class InteractionCreateEvent extends Event {
}

const context = command.createContext(client, args, interaction);
await command.execute(client, context);
await command.run(client, context);
} catch (error) {
const errorMessage = {
embeds: [Embeds.error(`${error instanceof Error ? error.message : 'Unknown error'}`)],
Expand Down
2 changes: 1 addition & 1 deletion src/events/MessageCreateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class MessageCreateEvent extends Event<'messageCreate'> {
const parsedArgs = await command.parseChatArgs(args, message.guild ?? undefined);

const context = command.createContext(client, parsedArgs, undefined, message);
await command.execute(client, context);
await command.run(client, context);
} catch (error) {
await message.reply({
embeds: [Embeds.error(`${error instanceof Error ? error.message : 'Unknown error'}`)],
Expand Down
Loading
Loading