Update role command to use new entity
This commit is contained in:
parent
a40af24b18
commit
2897606065
2 changed files with 48 additions and 25 deletions
|
@ -33,27 +33,34 @@ export default class Role extends Command {
|
||||||
// =======
|
// =======
|
||||||
|
|
||||||
private async UseDefault(context: ICommandContext) {
|
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) {
|
if (context.args.length == 0) {
|
||||||
await this.SendRolesList(context, rolesArray, context.message.guild!.id);
|
await this.SendRolesList(context, context.message.guild!.id);
|
||||||
} else {
|
} else {
|
||||||
await this.ToggleRole(context, rolesArray);
|
await this.ToggleRole(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async SendRolesList(context: ICommandContext, roles: String[], serverId: string): Promise<ICommandReturnContext> {
|
public async GetRolesList(context: ICommandContext): Promise<string[]> {
|
||||||
|
const rolesArray = await eRole.FetchAllByServerId(context.message.guild!.id);
|
||||||
|
|
||||||
|
const stringArray: string[] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < rolesArray.length; i++) {
|
||||||
|
const serverRole = context.message.guild!.roles.cache.find(x => x.id == rolesArray[i].RoleId);
|
||||||
|
|
||||||
|
if (serverRole) {
|
||||||
|
stringArray.push(serverRole.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async SendRolesList(context: ICommandContext, serverId: string): Promise<ICommandReturnContext> {
|
||||||
|
const roles = await this.GetRolesList(context);
|
||||||
|
|
||||||
const botPrefix = await SettingsHelper.GetServerPrefix(serverId);
|
const botPrefix = await SettingsHelper.GetServerPrefix(serverId);
|
||||||
const description = `Do ${botPrefix}role <role> to get the role!\n${roles.join('\n')}`;
|
const description = roles.length == 0 ? "*no roles*" : `Do ${botPrefix}role <role> to get the role!\n\n${roles.join('\n')}`;
|
||||||
|
|
||||||
const embed = new PublicEmbed(context, "Roles", description);
|
const embed = new PublicEmbed(context, "Roles", description);
|
||||||
embed.SendToCurrentChannel();
|
embed.SendToCurrentChannel();
|
||||||
|
@ -64,7 +71,8 @@ export default class Role extends Command {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async ToggleRole(context: ICommandContext, roles: String[]): Promise<ICommandReturnContext> {
|
public async ToggleRole(context: ICommandContext): Promise<ICommandReturnContext> {
|
||||||
|
const roles = await this.GetRolesList(context);
|
||||||
const requestedRole = context.args.join(" ");
|
const requestedRole = context.args.join(" ");
|
||||||
|
|
||||||
if (!roles.includes(requestedRole)) {
|
if (!roles.includes(requestedRole)) {
|
||||||
|
@ -168,7 +176,7 @@ export default class Role extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingRole = await eRole.FetchOneByServerId(context.message.guild!.id, role.id);
|
const existingRole = await eRole.FetchOneByRoleId(role.id);
|
||||||
|
|
||||||
if (existingRole) {
|
if (existingRole) {
|
||||||
const errorEmbed = new ErrorEmbed(context, "This role has already been setup");
|
const errorEmbed = new ErrorEmbed(context, "This role has already been setup");
|
||||||
|
@ -181,7 +189,12 @@ export default class Role extends Command {
|
||||||
"Roles",
|
"Roles",
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!server) return;
|
if (!server) {
|
||||||
|
const errorEmbed = new ErrorEmbed(context, "Server not setup, please request the server owner runs the setup command.");
|
||||||
|
errorEmbed.SendToCurrentChannel();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const roleSetting = new eRole(role.id);
|
const roleSetting = new eRole(role.id);
|
||||||
|
|
||||||
|
@ -202,7 +215,7 @@ export default class Role extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const existingRole = await eRole.FetchOneByServerId(context.message.guild!.id, role.id);
|
const existingRole = await eRole.FetchOneByRoleId(role.id);
|
||||||
|
|
||||||
if (!existingRole) {
|
if (!existingRole) {
|
||||||
const errorEmbed = new ErrorEmbed(context, "Unable to find this role");
|
const errorEmbed = new ErrorEmbed(context, "Unable to find this role");
|
||||||
|
|
|
@ -16,19 +16,29 @@ export default class Role extends BaseEntity {
|
||||||
@ManyToOne(() => Server, x => x.Roles)
|
@ManyToOne(() => Server, x => x.Roles)
|
||||||
Server: Server;
|
Server: Server;
|
||||||
|
|
||||||
public static async FetchOneByServerId(serverId: string, roleId: string): Promise<Role | undefined> {
|
public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise<Role | undefined> {
|
||||||
|
const connection = getConnection();
|
||||||
|
|
||||||
|
const repository = connection.getRepository(Role);
|
||||||
|
|
||||||
|
const single = await repository.findOne({ RoleId: roleId}, { relations: relations || [] });
|
||||||
|
|
||||||
|
return single;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async FetchAllByServerId(serverId: string): Promise<Role[]> {
|
||||||
const connection = getConnection();
|
const connection = getConnection();
|
||||||
|
|
||||||
const repository = connection.getRepository(Server);
|
const repository = connection.getRepository(Server);
|
||||||
|
|
||||||
const single = await repository.findOne(serverId, { relations: [
|
const all = await repository.findOne(serverId, { relations: [
|
||||||
"Roles",
|
"Roles",
|
||||||
]});
|
]});
|
||||||
|
|
||||||
if (!single) return undefined;
|
if (!all) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
const search = single.Roles.find(x => x.Id == roleId);
|
return all.Roles;
|
||||||
|
|
||||||
return search;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue