diff --git a/src/cmd/registry/roleinit.ts b/src/cmd/registry/roleinit.ts index a9e6353..fc55fd6 100644 --- a/src/cmd/registry/roleinit.ts +++ b/src/cmd/registry/roleinit.ts @@ -1,10 +1,15 @@ import type { Message } from "discord.js"; import type { Command } from "../../models"; +import RoleService from "../../services/role"; export default { name: "roleinit", description: "Creates all of the required roles, if they do not exist yet.", usage: ".roleinit", execute: async (message: Message) => { + if (!message.guild) return; + const reply = await message.reply("Initializing roles..."); + await RoleService.createRolesIfNotExist(message.guild, Object.values(RoleService.ROLES)); + await reply.edit("Finished initializing roles."); } } satisfies Command; diff --git a/src/index.ts b/src/index.ts index 71e319c..d70890c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,8 +22,13 @@ client.on("guildMemberAdd", async (guildMember) => { client.on("messageCreate", async (message) => { if (message.author.bot) return; - const command = CommandService.parse(message); - if (command) command.execute(message); + try { + const command = CommandService.parse(message); + if (command) command.execute(message); + } + catch (err) { + console.error(err); + } }); Bun.cron("0 */6 * * *", async () => { diff --git a/src/services/command.ts b/src/services/command.ts index b2309c7..c96f7b2 100644 --- a/src/services/command.ts +++ b/src/services/command.ts @@ -9,7 +9,7 @@ export default abstract class CommandService { static async init() { const commands = await Promise.all( - (await readdir(`${import.meta.dir}/registry`)).map(async file => (await import(`./registry/${file}`)).default satisfies Command) + (await readdir(`${import.meta.dir}/../cmd/registry`)).map(async file => (await import(`../cmd/registry/${file}`)).default satisfies Command) ); for (const command of commands) CommandService.registry[command.name] = command; diff --git a/src/services/role.ts b/src/services/role.ts index ae16937..8702214 100644 --- a/src/services/role.ts +++ b/src/services/role.ts @@ -8,52 +8,44 @@ export default abstract class RoleService { colors: { primaryColor: "#58a9df" }, hoist: true, mentionable: true, - position: 1, }, "FEMOID": { name: "femoid", colors: { primaryColor: "#ffb2f4" }, hoist: true, mentionable: true, - position: 2, }, "NORMALFAG": { name: "normalfag", colors: { primaryColor: "#e26254" }, hoist: true, mentionable: true, - position: 3, }, "GAY": { name: "gay", colors: { primaryColor: "#71368a" }, hoist: true, mentionable: true, - position: 4, }, "TROID": { name: "troid", colors: { primaryColor: "#992d22" }, hoist: true, mentionable: true, - position: 5, }, "LURKER": { name: "lurker", colors: { primaryColor: "#546e7a" }, hoist: true, mentionable: true, - position: 6, }, // Utility "NEWFAG": { name: "newfag", mentionable: true, - position: 7, }, "LIMBO": { name: "limbo", - position: 8, } }; @@ -66,10 +58,12 @@ export default abstract class RoleService { }); } - static async createRoles(guild: Guild, roles: RoleCreateOptions[]) { + static async createRolesIfNotExist(guild: Guild, roles: RoleCreateOptions[]) { for (const role of roles) { try { - await guild.roles.create(role); + if (!this.getRoleByName(guild, role.name)) { + await guild.roles.create(role); + } } catch (err) { console.error(`Failed to create role: ${role.name}`, err);