137 role command cannot read properties of undefined #141

Merged
Vylpes merged 5 commits from 137-role-command-cannot-read-properties-of-undefined into develop 2022-04-23 18:43:30 +01:00
7 changed files with 141 additions and 14 deletions

View file

@ -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")

8
data/usage/role.txt Normal file
View file

@ -0,0 +1,8 @@
USAGE: config <add|remove> <Role ID>
===[ EXAMPLE ]===
To add a role:
- config add 000000000000000000
To remove a role:
- config remove 000000000000000000

View file

@ -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();

View file

@ -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);

View file

@ -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 <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<ICommandReturnContext> {
const botPrefix = await SettingsHelper.GetServerPrefix(serverId);
const description = `Do ${botPrefix}role <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<ICommandReturnContext> {
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]
};
}
}
// ======
// 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();
}
}

View file

@ -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<string> {
const setting = await this.GetSetting("bot.prefix", serverId);
if (!setting) {
return "v!";
}
VylpesTester commented 2022-04-23 18:31:11 +01:00 (Migrated from github.com)
Review

Not needed

Not needed
return setting;
}
}