From 62dc3a8d6e2e91af4527a41582a00a603bdd347e Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 23 Apr 2022 17:28:03 +0100 Subject: [PATCH 1/5] Fix issue with bot crashing --- data/config.txt | 2 +- src/commands/role.ts | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/data/config.txt b/data/config.txt index 3156fb2..8b9bce3 100644 --- a/data/config.txt +++ b/data/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/src/commands/role.ts b/src/commands/role.ts index 28586ed..4f48433 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -4,6 +4,7 @@ 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"; export default class Role extends Command { constructor() { @@ -13,12 +14,23 @@ 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; + + 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) { - this.SendRolesList(context, roles); + this.SendRolesList(context, rolesArray); } else { - await this.ToggleRole(context, roles); + await this.ToggleRole(context, rolesArray); } } -- 2.43.4 From ae1b5f237ce2ba943fba9f38b2c02ed2006301aa Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 23 Apr 2022 17:34:37 +0100 Subject: [PATCH 2/5] Fix server prefix not showing --- src/commands/role.ts | 7 ++++--- src/helpers/SettingsHelper.ts | 11 ++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/commands/role.ts b/src/commands/role.ts index 4f48433..12811bb 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -28,14 +28,15 @@ export default class Role extends Command { const rolesArray = roles.split(","); if (context.args.length == 0) { - this.SendRolesList(context, rolesArray); + await this.SendRolesList(context, rolesArray, context.message.guild.id); } else { await this.ToggleRole(context, rolesArray); } } - public SendRolesList(context: ICommandContext, roles: String[]): ICommandReturnContext { - const description = `Do ${process.env.BOT_PREFIX}role to get the role!\n${roles.join('\n')}`; + 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(); 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 -- 2.43.4 From 55f6ce82650b43aa4d2ec5c9ee7c02d2a6bbc3aa Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 23 Apr 2022 18:04:27 +0100 Subject: [PATCH 3/5] Add easy way to configure role command --- data/roleConfig.txt | 8 ++++ src/commands/role.ts | 105 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 data/roleConfig.txt diff --git a/data/roleConfig.txt b/data/roleConfig.txt new file mode 100644 index 0000000..7173364 --- /dev/null +++ b/data/roleConfig.txt @@ -0,0 +1,8 @@ +USAGE: config + +===[ EXAMPLE ]=== +To add a role: +- config add role-name + +To remove a role: +- config remove role-name \ No newline at end of file diff --git a/src/commands/role.ts b/src/commands/role.ts index 12811bb..96807ab 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -5,7 +5,7 @@ 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(); @@ -16,7 +16,21 @@ export default class Role extends Command { public override async execute(context: ICommandContext) { if (!context.message.guild) return; - const roles = await SettingsHelper.GetSetting("role.assignable", context.message.guild.id); + switch (context.args[0]) { + case "config": + await this.UseConfig(context); + break; + default: + await this.UseDefault(context); + } + } + + // ======= + // 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"); @@ -28,7 +42,7 @@ export default class Role extends Command { const rolesArray = roles.split(","); if (context.args.length == 0) { - await this.SendRolesList(context, rolesArray, context.message.guild.id); + await this.SendRolesList(context, rolesArray, context.message.guild!.id); } else { await this.ToggleRole(context, rolesArray); } @@ -109,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/roleConfig.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.name == 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.name == 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 == context.args[2]); + + 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(); + } +} -- 2.43.4 From 11a0e7564ae05e7af2827c70452c42d515dd7e7a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 23 Apr 2022 18:37:55 +0100 Subject: [PATCH 4/5] Move help text to its own directory --- data/{ => usage}/config.txt | 0 data/{lobbyConfig.txt => usage/lobby.txt} | 0 data/{roleConfig.txt => usage/role.txt} | 0 src/commands/501231711271780357/lobby.ts | 2 +- src/commands/config.ts | 2 +- src/commands/role.ts | 2 +- 6 files changed, 3 insertions(+), 3 deletions(-) rename data/{ => usage}/config.txt (100%) rename data/{lobbyConfig.txt => usage/lobby.txt} (100%) rename data/{roleConfig.txt => usage/role.txt} (100%) diff --git a/data/config.txt b/data/usage/config.txt similarity index 100% rename from data/config.txt rename to data/usage/config.txt 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/roleConfig.txt b/data/usage/role.txt similarity index 100% rename from data/roleConfig.txt rename to data/usage/role.txt diff --git a/src/commands/501231711271780357/lobby.ts b/src/commands/501231711271780357/lobby.ts index 78ca89b..35cdd9c 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 96807ab..3d54825 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -151,7 +151,7 @@ export default class Role extends Command { } private SendConfigHelp(context: ICommandContext) { - const helpText = readFileSync(`${process.cwd()}/data/roleConfig.txt`).toString(); + const helpText = readFileSync(`${process.cwd()}/data/usage/role.txt`).toString(); const embed = new PublicEmbed(context, "Configure Role Command", helpText); embed.SendToCurrentChannel(); -- 2.43.4 From b22048eb14716eb9cf6021ad97905b5beb31e4aa Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 23 Apr 2022 18:43:00 +0100 Subject: [PATCH 5/5] Make role config command to use role id --- data/usage/role.txt | 6 +++--- src/commands/role.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/data/usage/role.txt b/data/usage/role.txt index 7173364..0e8aa07 100644 --- a/data/usage/role.txt +++ b/data/usage/role.txt @@ -1,8 +1,8 @@ -USAGE: config +USAGE: config ===[ EXAMPLE ]=== To add a role: -- config add role-name +- config add 000000000000000000 To remove a role: -- config remove role-name \ No newline at end of file +- config remove 000000000000000000 \ No newline at end of file diff --git a/src/commands/role.ts b/src/commands/role.ts index 3d54825..6db403b 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -62,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"); @@ -158,7 +158,7 @@ export default class Role extends Command { } private async AddRoleConfig(context: ICommandContext) { - const role = context.message.guild!.roles.cache.find(x => x.name == context.args[2]); + const role = context.message.guild!.roles.cache.find(x => x.id == context.args[2]); if (!role) { this.SendConfigHelp(context); @@ -180,7 +180,7 @@ export default class Role extends Command { } private async RemoveRoleConfig(context: ICommandContext) { - const role = context.message.guild!.roles.cache.find(x => x.name == context.args[2]); + const role = context.message.guild!.roles.cache.find(x => x.id == context.args[2]); if (!role) { this.SendConfigHelp(context); @@ -193,7 +193,7 @@ export default class Role extends Command { const settingArray = setting.split(","); - const index = settingArray.findIndex(x => x == context.args[2]); + const index = settingArray.findIndex(x => x == role.name); if (index == -1) return; -- 2.43.4