150 assignable roles should be its own table to prevent limitations on length #158
2 changed files with 45 additions and 19 deletions
|
@ -6,6 +6,9 @@ import { ICommandContext } from "../contracts/ICommandContext";
|
|||
import ICommandReturnContext from "../contracts/ICommandReturnContext";
|
||||
import SettingsHelper from "../helpers/SettingsHelper";
|
||||
import { readFileSync } from "fs";
|
||||
import { default as eRole } from "../entity/Role";
|
||||
import Server from "../entity/Server";
|
||||
|
||||
export default class Role extends Command {
|
||||
constructor() {
|
||||
super();
|
||||
|
@ -165,15 +168,27 @@ export default class Role extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id) || "";
|
||||
const existingRole = await eRole.FetchOneByServerId(context.message.guild!.id, role.id);
|
||||
|
||||
const settingArray = setting.split(",");
|
||||
if (existingRole) {
|
||||
const errorEmbed = new ErrorEmbed(context, "This role has already been setup");
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
|
||||
settingArray.push(role.name);
|
||||
return;
|
||||
}
|
||||
|
||||
setting = settingArray.join(",");
|
||||
const server = await Server.FetchOneById(Server, context.message.guild!.id, [
|
||||
"Roles",
|
||||
]);
|
||||
|
||||
await SettingsHelper.SetSetting("role.assignable", context.message.guild!.id, setting);
|
||||
if (!server) return;
|
||||
|
||||
const roleSetting = new eRole(role.id);
|
||||
|
||||
await roleSetting.Save(eRole, roleSetting);
|
||||
|
||||
server.AddRoleToServer(roleSetting);
|
||||
await server.Save(Server, server);
|
||||
|
||||
const embed = new PublicEmbed(context, "", `Added \`${role.name}\` as a new assignable role`);
|
||||
embed.SendToCurrentChannel();
|
||||
|
@ -187,21 +202,16 @@ export default class Role extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id);
|
||||
const existingRole = await eRole.FetchOneByServerId(context.message.guild!.id, role.id);
|
||||
|
||||
if (!setting) return;
|
||||
if (!existingRole) {
|
||||
const errorEmbed = new ErrorEmbed(context, "Unable to find this role");
|
||||
errorEmbed.SendToCurrentChannel();
|
||||
|
||||
const settingArray = setting.split(",");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
await eRole.Remove(eRole, existingRole);
|
||||
|
||||
const embed = new PublicEmbed(context, "", `Removed \`${role.name}\` from the list of assignable roles`);
|
||||
embed.SendToCurrentChannel();
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
import { Column, Entity, EntityTarget, getConnection, ManyToOne } from "typeorm";
|
||||
import BaseEntity from "../contracts/BaseEntity"
|
||||
import Server from "./Server";
|
||||
|
||||
|
@ -15,4 +15,20 @@ export default class Role extends BaseEntity {
|
|||
|
||||
@ManyToOne(() => Server, x => x.Roles)
|
||||
Server: Server;
|
||||
|
||||
public static async FetchOneByServerId(serverId: string, roleId: string): Promise<Role | undefined> {
|
||||
const connection = getConnection();
|
||||
|
||||
const repository = connection.getRepository(Server);
|
||||
|
||||
const single = await repository.findOne(serverId, { relations: [
|
||||
"Roles",
|
||||
]});
|
||||
|
||||
if (!single) return undefined;
|
||||
|
||||
const search = single.Roles.find(x => x.Id == roleId);
|
||||
|
||||
return search;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue