diff --git a/data/config.txt b/data/usage/config.txt similarity index 92% rename from data/config.txt rename to data/usage/config.txt index 3156fb2..8b9bce3 100644 --- a/data/config.txt +++ b/data/usage/config.txt @@ -5,7 +5,7 @@ bot.prefix: The bot prefix for the server (Default: "v!") commands.disabled: Disabled commands, separated by commas (Default: "") -role.assignable: List of roles assignable to user (Default: []) +role.assignable: List of roles assignable to user, separated by commas (Default: "") role.moderator: The moderator role name (Default: "Moderator") role.administrator: The administrator role name (Default: "Administrator") role.muted: The muted role name (Default: "Muted") diff --git a/data/lobbyConfig.txt b/data/usage/lobby.txt similarity index 100% rename from data/lobbyConfig.txt rename to data/usage/lobby.txt diff --git a/data/usage/role.txt b/data/usage/role.txt new file mode 100644 index 0000000..0e8aa07 --- /dev/null +++ b/data/usage/role.txt @@ -0,0 +1,8 @@ +USAGE: config + +===[ EXAMPLE ]=== +To add a role: +- config add 000000000000000000 + +To remove a role: +- config remove 000000000000000000 \ No newline at end of file diff --git a/src/commands/501231711271780357/lobby.ts b/src/commands/501231711271780357/lobby.ts index 74ca244..c4b4bea 100644 --- a/src/commands/501231711271780357/lobby.ts +++ b/src/commands/501231711271780357/lobby.ts @@ -99,7 +99,7 @@ export default class Lobby extends Command { } private SendConfigHelp(context: ICommandContext) { - const helpText = readFileSync(`${process.cwd()}/data/lobbyConfig.txt`).toString(); + const helpText = readFileSync(`${process.cwd()}/data/usage/lobby.txt`).toString(); const embed = new PublicEmbed(context, "Configure Lobby Command", helpText); embed.SendToCurrentChannel(); diff --git a/src/commands/config.ts b/src/commands/config.ts index aa46263..b7e9c93 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -79,7 +79,7 @@ export default class Config extends Command { } private async SendHelpText(context: ICommandContext) { - const description = readFileSync(`${process.cwd()}/data/config.txt`).toString(); + const description = readFileSync(`${process.cwd()}/data/usage/config.txt`).toString(); const embed = new PublicEmbed(context, "Config", description); diff --git a/src/commands/role.ts b/src/commands/role.ts index 28586ed..6db403b 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -4,7 +4,8 @@ import { Role as DiscordRole } from "discord.js"; import { Command } from "../type/command"; import { ICommandContext } from "../contracts/ICommandContext"; import ICommandReturnContext from "../contracts/ICommandReturnContext"; - +import SettingsHelper from "../helpers/SettingsHelper"; +import { readFileSync } from "fs"; export default class Role extends Command { constructor() { super(); @@ -13,17 +14,43 @@ export default class Role extends Command { } public override async execute(context: ICommandContext) { - const roles = process.env.COMMANDS_ROLE_ROLES!.split(','); + if (!context.message.guild) return; - if (context.args.length == 0) { - this.SendRolesList(context, roles); - } else { - await this.ToggleRole(context, roles); + switch (context.args[0]) { + case "config": + await this.UseConfig(context); + break; + default: + await this.UseDefault(context); } } - public SendRolesList(context: ICommandContext, roles: String[]): ICommandReturnContext { - const description = `Do ${process.env.BOT_PREFIX}role to get the role!\n${roles.join('\n')}`; + // ======= + // Default + // ======= + + private async UseDefault(context: ICommandContext) { + const roles = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id); + + if (!roles) { + const errorEmbed = new ErrorEmbed(context, "Unable to find any assignable roles"); + errorEmbed.SendToCurrentChannel(); + + return; + } + + const rolesArray = roles.split(","); + + if (context.args.length == 0) { + await this.SendRolesList(context, rolesArray, context.message.guild!.id); + } else { + await this.ToggleRole(context, rolesArray); + } + } + + public async SendRolesList(context: ICommandContext, roles: String[], serverId: string): Promise { + const botPrefix = await SettingsHelper.GetServerPrefix(serverId); + const description = `Do ${botPrefix}role to get the role!\n${roles.join('\n')}`; const embed = new PublicEmbed(context, "Roles", description); embed.SendToCurrentChannel(); @@ -35,7 +62,7 @@ export default class Role extends Command { } public async ToggleRole(context: ICommandContext, roles: String[]): Promise { - const requestedRole = context.args[0]; + const requestedRole = context.args.join(" "); if (!roles.includes(requestedRole)) { const errorEmbed = new ErrorEmbed(context, "This role isn't marked as assignable, to see a list of assignable roles, run this command without any parameters"); @@ -96,4 +123,87 @@ export default class Role extends Command { embeds: [embed] }; } -} \ No newline at end of file + + // ====== + // Config + // ====== + + private async UseConfig(context: ICommandContext) { + const moderatorRole = await SettingsHelper.GetSetting("role.moderator", context.message.guild!.id); + + if (!context.message.member?.roles.cache.find(x => x.name == moderatorRole)) { + const errorEmbed = new ErrorEmbed(context, "Sorry, you must be a moderator to be able to configure this command"); + errorEmbed.SendToCurrentChannel(); + + return; + } + + switch (context.args[1]) { + case "add": + await this.AddRoleConfig(context); + break; + case "remove": + await this.RemoveRoleConfig(context); + break; + default: + this.SendConfigHelp(context); + } + } + + private SendConfigHelp(context: ICommandContext) { + const helpText = readFileSync(`${process.cwd()}/data/usage/role.txt`).toString(); + + const embed = new PublicEmbed(context, "Configure Role Command", helpText); + embed.SendToCurrentChannel(); + } + + private async AddRoleConfig(context: ICommandContext) { + const role = context.message.guild!.roles.cache.find(x => x.id == context.args[2]); + + if (!role) { + this.SendConfigHelp(context); + return; + } + + let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id) || ""; + + const settingArray = setting.split(","); + + settingArray.push(role.name); + + setting = settingArray.join(","); + + await SettingsHelper.SetSetting("role.assignable", context.message.guild!.id, setting); + + const embed = new PublicEmbed(context, "", "Added new assignable role"); + embed.SendToCurrentChannel(); + } + + private async RemoveRoleConfig(context: ICommandContext) { + const role = context.message.guild!.roles.cache.find(x => x.id == context.args[2]); + + if (!role) { + this.SendConfigHelp(context); + return; + } + + let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id); + + if (!setting) return; + + const settingArray = setting.split(","); + + const index = settingArray.findIndex(x => x == role.name); + + if (index == -1) return; + + settingArray.splice(index, 1); + + setting = settingArray.join(","); + + await SettingsHelper.SetSetting("role.assignable", context.message.guild!.id, setting); + + const embed = new PublicEmbed(context, "", "Removed assignable role"); + embed.SendToCurrentChannel(); + } +} diff --git a/src/helpers/SettingsHelper.ts b/src/helpers/SettingsHelper.ts index 35bc342..1c6fd6a 100644 --- a/src/helpers/SettingsHelper.ts +++ b/src/helpers/SettingsHelper.ts @@ -1,4 +1,3 @@ -import { getConnection } from "typeorm"; import DefaultValues from "../constants/DefaultValues"; import Server from "../entity/Server"; import Setting from "../entity/Setting"; @@ -47,4 +46,14 @@ export default class SettingsHelper { await server.Save(Server, server); } } + + public static async GetServerPrefix(serverId: string): Promise { + const setting = await this.GetSetting("bot.prefix", serverId); + + if (!setting) { + return "v!"; + } + + return setting; + } } \ No newline at end of file