150 assignable roles should be its own table to prevent limitations on length #158

Merged
Vylpes merged 4 commits from 150-assignable-roles-should-be-its-own-table-to-prevent-limitations-on-length into develop 2022-05-14 14:59:33 +01:00
2 changed files with 45 additions and 19 deletions
Showing only changes of commit a40af24b18 - Show all commits

View file

@ -6,6 +6,9 @@ import { ICommandContext } from "../contracts/ICommandContext";
import ICommandReturnContext from "../contracts/ICommandReturnContext"; import ICommandReturnContext from "../contracts/ICommandReturnContext";
import SettingsHelper from "../helpers/SettingsHelper"; import SettingsHelper from "../helpers/SettingsHelper";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { default as eRole } from "../entity/Role";
import Server from "../entity/Server";
export default class Role extends Command { export default class Role extends Command {
constructor() { constructor() {
super(); super();
@ -164,16 +167,28 @@ export default class Role extends Command {
this.SendConfigHelp(context); this.SendConfigHelp(context);
return; return;
} }
let setting = await SettingsHelper.GetSetting("role.assignable", context.message.guild!.id) || "";
const settingArray = setting.split(","); const existingRole = await eRole.FetchOneByServerId(context.message.guild!.id, role.id);
settingArray.push(role.name); if (existingRole) {
const errorEmbed = new ErrorEmbed(context, "This role has already been setup");
errorEmbed.SendToCurrentChannel();
setting = settingArray.join(","); return;
}
await SettingsHelper.SetSetting("role.assignable", context.message.guild!.id, setting); const server = await Server.FetchOneById(Server, context.message.guild!.id, [
"Roles",
]);
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`); const embed = new PublicEmbed(context, "", `Added \`${role.name}\` as a new assignable role`);
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();
@ -187,21 +202,16 @@ export default class Role extends Command {
return; 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); await eRole.Remove(eRole, existingRole);
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 \`${role.name}\` from the list of assignable roles`); const embed = new PublicEmbed(context, "", `Removed \`${role.name}\` from the list of assignable roles`);
embed.SendToCurrentChannel(); embed.SendToCurrentChannel();

View file

@ -1,4 +1,4 @@
import { Column, Entity, ManyToOne } from "typeorm"; import { Column, Entity, EntityTarget, getConnection, ManyToOne } from "typeorm";
import BaseEntity from "../contracts/BaseEntity" import BaseEntity from "../contracts/BaseEntity"
import Server from "./Server"; import Server from "./Server";
@ -15,4 +15,20 @@ 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> {
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;
}
} }