diff --git a/src/commands/role.ts b/src/commands/role.ts index 9a0af00..3112730 100644 --- a/src/commands/role.ts +++ b/src/commands/role.ts @@ -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(); @@ -164,16 +167,28 @@ export default class Role extends Command { this.SendConfigHelp(context); 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`); 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(); diff --git a/src/entity/Role.ts b/src/entity/Role.ts index 05babcb..5770ab1 100644 --- a/src/entity/Role.ts +++ b/src/entity/Role.ts @@ -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 { + 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; + } } \ No newline at end of file