public async Task IAmNotAsync(EventContext e) { IDiscordRole role = GetRoleByName(e.Guild, e.arguments); if (role == null) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_null")) .SendToChannel(e.Channel); return; } if (!e.Author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_forbidden")) .SendToChannel(e.Channel); return; } using (var context = new MikiContext()) { LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); await e.Author.RemoveRoleAsync(newRole.Role); Utils.Embed.SetTitle("I AM NOT") .SetColor(255, 128, 128) .SetDescription($"You're no longer a(n) {role.Name}!") .QueueToChannel(e.Channel); } }
public async Task IAmNotAsync(EventContext e) { string roleName = e.Arguments.ToString(); using (var context = new MikiContext()) { List <IDiscordRole> roles = await GetRolesByName(e.Guild, roleName); IDiscordRole role = null; if (roles.Count > 1) { List <LevelRole> levelRoles = await context.LevelRoles.Where(x => x.GuildId == (long)e.Guild.Id).ToListAsync(); if (levelRoles.Where(x => x.Role.Name.ToLower() == roleName.ToLower()).Count() > 1) { e.ErrorEmbed("two roles configured have the same name.") .ToEmbed().QueueToChannel(e.Channel); return; } else { role = levelRoles.Where(x => x.Role.Name.ToLower() == roleName.ToLower()).FirstOrDefault().Role; } } else { role = roles.FirstOrDefault(); } if (role == null) { await e.ErrorEmbed(e.GetResource("error_role_null")) .ToEmbed().SendToChannel(e.Channel); return; } IDiscordGuildUser author = await e.Guild.GetUserAsync(e.Author.Id); if (!author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(e.GetResource("error_role_forbidden")) .ToEmbed().SendToChannel(e.Channel); return; } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); await author.RemoveRoleAsync(newRole.Role); Utils.Embed.SetTitle("I AM NOT") .SetColor(255, 128, 128) .SetDescription($"You're no longer a(n) {role.Name}!") .ToEmbed().QueueToChannel(e.Channel); } }
private void RequiredLevelValid(LevelRole role, LocalExperience localUser) { int level = User.CalculateLevel(localUser.Experience); if (role.RequiredLevel > level) { throw new LevelTooLowException(level, role.RequiredLevel); } }
public async Task SetRoleLevelAsync(EventContext e) { using (var context = new MikiContext()) { Locale locale = Locale.GetEntity(e.Channel.Id.ToDbLong()); List <string> allArgs = new List <string>(); allArgs.AddRange(e.arguments.Split(' ')); if (allArgs.Count >= 2) { int levelrequirement = int.Parse(allArgs[allArgs.Count - 1]); allArgs.RemoveAt(allArgs.Count - 1); IDiscordRole role = e.Guild.Roles.Find(r => r.Name.ToLower() == string.Join(" ", allArgs).TrimEnd(' ').TrimStart(' ').ToLower()); if (role == null) { await Utils.ErrorEmbed(locale, "Couldn't find this role. Please try again!").SendToChannel(e.Channel); return; } LevelRole lr = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); if (lr == null) { lr = context.LevelRoles.Add(new LevelRole() { GuildId = e.Guild.Id.ToDbLong(), RoleId = role.Id.ToDbLong(), RequiredLevel = levelrequirement }); IDiscordEmbed embed = Utils.Embed; embed.Title = "Added Role!"; embed.Description = $"I'll give someone the role {role.Name} when he/she reaches level {levelrequirement}!"; await embed.SendToChannel(e.Channel); } else { lr.RequiredLevel = levelrequirement; IDiscordEmbed embed = Utils.Embed; embed.Title = "Updated Role!"; embed.Description = $"I'll give someone the role {role.Name} when he/she reaches level {levelrequirement}!"; await embed.SendToChannel(e.Channel); } await context.SaveChangesAsync(); } else { await Utils.ErrorEmbed(locale, "Make sure to fill out both the role and the level when creating this!").SendToChannel(e.Channel); } } }
public async Task ShouldRemoveLevelRole_WhenLevelRolesExists() { const int id = 5; LevelRole role = null; var mockDbSet = _dbContext.ConfigureMockDbSet(x => x.LevelRoles, new LevelRole { Id = id }); mockDbSet.When(x => x.Remove(Arg.Any <LevelRole>())).Do(x => role = x.Arg <LevelRole>()); await _appFixture.SendAsync(new RemoveLevelRoleCommand { Id = id }); await _dbContext.Received(1).SaveChangesAsync(default);
public async Task ShouldAddLevelRole_WhenNoLevelRolesExist() { ulong roleId = 5; var level = 45; LevelRole addedRole = null; var mockDbSet = _dbContext.ConfigureMockDbSet(x => x.LevelRoles); mockDbSet.When(x => x.AddAsync(Arg.Any <LevelRole>())).Do(x => addedRole = x.Arg <LevelRole>()); await _appFixture.SendAsync(new AddLevelRoleCommand { RoleId = roleId, Level = level, Remain = true }); await _dbContext.Received(1).SaveChangesAsync(default);
public async Task IAmAsync(IContext e) { var context = e.GetService <MikiDbContext>(); var userService = e.GetService <IUserService>(); var transactionService = e.GetService <ITransactionService>(); var locale = e.GetLocale(); string roleName = e.GetArgumentPack().Pack.TakeAll(); List <IDiscordRole> roles = await GetRolesByNameAsync(e.GetGuild(), roleName); IDiscordRole role; // checking if the role has a duplicate name. if (roles.Count > 1) { var roleIds = roles.Select(x => (long)x.Id); List <LevelRole> levelRoles = await context.LevelRoles .Where(x => roleIds.Contains(x.RoleId)) .ToListAsync(); if (!levelRoles.Any()) { return; } if (levelRoles.Count > 1) { await e.ErrorEmbed("two roles configured have the same name.") .ToEmbed().QueueAsync(e, e.GetChannel()); return; } role = roles.FirstOrDefault(x => levelRoles.First().RoleId == (long)x.Id); } else { role = roles.FirstOrDefault(); } if (role == null) { throw new RoleNullException(); } if (!(e.GetAuthor() is IDiscordGuildUser author)) { throw new InvalidCastException("User was not proper Guild Member"); } if (author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(locale.GetString("error_role_already_given")) .ToEmbed() .QueueAsync(e, e.GetChannel()); return; } LevelRole newRole = await context.LevelRoles.FindAsync( e.GetGuild().Id.ToDbLong(), role.Id.ToDbLong()); if (newRole == null) { throw new RoleNotSetupException(); } User user = await userService.GetOrCreateUserAsync(e.GetAuthor()); var localUser = await LocalExperience.GetAsync( context, e.GetGuild().Id, author.Id); if (localUser == null) { localUser = await LocalExperience.CreateAsync( context, e.GetGuild().Id, author.Id, author.Username); } if (!newRole.Optable) { await e.ErrorEmbed(e.GetLocale().GetString("error_role_forbidden")) .ToEmbed() .QueueAsync(e, e.GetChannel()); return; } RequiredLevelValid(newRole, localUser); if (newRole.RequiredRole != 0 && !author.RoleIds.Contains(newRole.RequiredRole.FromDbLong())) { var requiredRole = await e.GetGuild().GetRoleAsync(newRole.RequiredRole.FromDbLong()); throw new RequiredRoleMissingException(requiredRole); } if (newRole.Price > 0) { await transactionService.CreateTransactionAsync( new TransactionRequest.Builder() .WithAmount(newRole.Price) .WithReceiver(AppProps.Currency.BankId) .WithSender(user.Id) .Build()); } var me = await e.GetGuild().GetSelfAsync(); if (!await me.HasPermissionsAsync(GuildPermission.ManageRoles)) { await e.ErrorEmbed(locale.GetString("permission_missing", "give roles")).ToEmbed() .QueueAsync(e, e.GetChannel()); return; } int hierarchy = await me.GetHierarchyAsync(); if (role.Position >= hierarchy) { await e.ErrorEmbed(e.GetLocale().GetString("permission_error_low", "give roles")).ToEmbed() .QueueAsync(e, e.GetChannel()); return; } await author.AddRoleAsync(role); await new EmbedBuilder() .SetTitle("I AM") .SetColor(128, 255, 128) .SetDescription($"You're a(n) {role.Name} now!") .ToEmbed() .QueueAsync(e, e.GetChannel()); }
public async Task IAmNotAsync(IContext e) { string roleName = e.GetArgumentPack().Pack.TakeAll(); var context = e.GetService <MikiDbContext>(); List <IDiscordRole> roles = await GetRolesByNameAsync(e.GetGuild(), roleName); IDiscordRole role; if (roles.Count > 1) { List <LevelRole> levelRoles = await context.LevelRoles .Where(x => x.GuildId == (long)e.GetGuild().Id).ToListAsync(); if (levelRoles.Count( x => ModelExtensions.GetRoleAsync(x, e.GetGuild()).Result.Name.ToLower() == roleName.ToLower()) > 1) { await e.ErrorEmbed("two roles configured have the same name.") .ToEmbed().QueueAsync(e, e.GetChannel()); return; } else { role = await levelRoles .FirstOrDefault( x => x.GetRoleAsync(e.GetGuild()).Result.Name.ToLower() == roleName.ToLower()) .GetRoleAsync(e.GetGuild()); } } else { role = roles.FirstOrDefault(); } if (role == null) { await e.ErrorEmbed(e.GetLocale().GetString("error_role_null")) .ToEmbed().QueueAsync(e, e.GetChannel()); return; } IDiscordGuildUser author = await e.GetGuild().GetMemberAsync(e.GetAuthor().Id); IDiscordGuildUser me = await e.GetGuild().GetSelfAsync(); if (!author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(e.GetLocale().GetString("error_role_forbidden")) .ToEmbed().QueueAsync(e, e.GetChannel()); return; } LevelRole newRole = await context.LevelRoles.FindAsync(e.GetGuild().Id.ToDbLong(), role.Id.ToDbLong()); User user = await context.Users.FindAsync(e.GetAuthor().Id.ToDbLong()); if (!await me.HasPermissionsAsync(GuildPermission.ManageRoles)) { await e.ErrorEmbed(e.GetLocale().GetString("permission_error_low", "give roles")) .ToEmbed() .QueueAsync(e, e.GetChannel()); return; } if (role.Position >= await me.GetHierarchyAsync()) { await e.ErrorEmbed(e.GetLocale().GetString("permission_error_low", "give roles")) .ToEmbed() .QueueAsync(e, e.GetChannel()); return; } await author.RemoveRoleAsync(role); await new EmbedBuilder() .SetTitle("I AM NOT") .SetColor(255, 128, 128) .SetDescription($"You're no longer a(n) {role.Name}!") .ToEmbed().QueueAsync(e, e.GetChannel()); }
public static async Task <IDiscordRole> GetRoleAsync(this LevelRole role, IDiscordGuild guild) { return(await guild.GetRoleAsync((ulong)role.RoleId)); }
public async Task ConfigRoleInteractiveAsync(EventContext e) { using (var context = new MikiContext()) { EmbedBuilder sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription("Type out the role name you want to config") .SetColor(138, 182, 239); IDiscordMessage sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); IDiscordMessage msg = null; while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (msg.Content.Length < 20) { break; } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed("That role name is way too long! Try again.") .ToEmbed() }); } } string roleName = msg.Content; List <IDiscordRole> rolesFound = await GetRolesByName(e.Guild, roleName.ToLower()); IDiscordRole role = null; if (rolesFound.Count == 0) { // Hey, I couldn't find this role, Can I make a new one? await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed($"There's no role that is named `{roleName}`, Shall I create it? Y/N").ToEmbed() }); msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (msg.Content.ToLower()[0] != 'y') { throw new RoleNullException(); } role = await e.Guild.CreateRoleAsync(new CreateRoleArgs { Name = roleName, }); } else if (rolesFound.Count > 1) { string roleIds = string.Join("\n", rolesFound.Select(x => $"`{x.Name}`: {x.Id}")); if (roleIds.Length > 1024) { roleIds = roleIds.Substring(0, 1024); } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription("I found multiple roles with that name, which one would you like? please enter the ID") .AddInlineField("Roles - Ids", roleIds) .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (ulong.TryParse(msg.Content, out ulong id)) { role = rolesFound.Where(x => x.Id == id) .FirstOrDefault(); if (role != null) { break; } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed("I couldn't find that role id in the list. Try again!") .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() }); } } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed("I couldn't find that role. Try again!") .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() }); } } } else { role = rolesFound.FirstOrDefault(); } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); if (newRole == null) { newRole = (await context.LevelRoles.AddAsync(new LevelRole() { RoleId = role.Id.ToDbLong(), GuildId = e.Guild.Id.ToDbLong() })).Entity; } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription("Is there a role that is needed to get this role? Type out the role name, or `-` to skip") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); rolesFound = (await GetRolesByName(e.Guild, msg.Content.ToLower())); IDiscordRole parentRole = null; if (rolesFound.Count > 1) { string roleIds = string.Join("\n", rolesFound.Select(x => $"`{x.Name}`: {x.Id}")); if (roleIds.Length > 1024) { roleIds = roleIds.Substring(0, 1024); } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription("I found multiple roles with that name, which one would you like? please enter the ID") .AddInlineField("Roles - Ids", roleIds) .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (ulong.TryParse(msg.Content, out ulong id)) { parentRole = rolesFound.Where(x => x.Id == id) .FirstOrDefault(); if (parentRole != null) { break; } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed("I couldn't find that role id in the list. Try again!") .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() }); } } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed("I couldn't find that role. Try again!") .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() }); } } } else { parentRole = rolesFound.FirstOrDefault(); } if (parentRole != null || msg.Content == "-") { newRole.RequiredRole = (long?)parentRole?.Id ?? 0; break; } await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed("I couldn't find that role. Try again!").ToEmbed() }); } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Is there a level requirement? type a number, if there is no requirement type 0") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (int.TryParse(msg.Content, out int r)) { if (r >= 0) { newRole.RequiredLevel = r; break; } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = sourceEmbed.SetDescription($"Please pick a number above 0. Try again") .ToEmbed() }); } } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = sourceEmbed.SetDescription($"Are you sure `{msg.Content}` is a number? Try again").ToEmbed() }); } } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Should I give them when the user level ups? type `yes`, otherwise it will be considered as no") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (msg == null) { return; } newRole.Automatic = msg.Content.ToLower()[0] == 'y'; sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Should users be able to opt in? type `yes`, otherwise it will be considered as no") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); newRole.Optable = msg.Content.ToLower()[0] == 'y'; if (newRole.Optable) { sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Do you want the user to pay mekos for the role? Enter any amount. Enter 0 to keep the role free.") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (msg == null) { return; } if (int.TryParse(msg.Content, out int r)) { if (r >= 0) { newRole.Price = r; break; } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed($"Please pick a number above 0. Try again").ToEmbed() }); } } else { await sourceMessage.EditAsync(new EditMessageArgs { embed = e.ErrorEmbed($"Not quite sure if this is a number 🤔").ToEmbed() }); } } } await context.SaveChangesAsync(); Utils.Embed.SetTitle("⚙ Role Config") .SetColor(102, 117, 127) .SetDescription($"Updated {role.Name}!") .ToEmbed().QueueToChannel(e.Channel); } }
/*public async Task ConfigRoleInteractiveAsync(EventContext e) * { * using (var context = new MikiContext()) * { * EmbedBuilder sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription("Type out the role name you want to config") * .SetColor(138, 182, 239); * IDiscordMessage sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * IDiscordMessage msg = null; * * while (true) * { * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * * if (msg.Content.Length < 20) * { * break; * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed("That role name is way too long! Try again.") * .ToEmbed() * }); * } * } * * string roleName = msg.Content; * * List<IDiscordRole> rolesFound = await GetRolesByName(e.Guild, roleName.ToLower()); * IDiscordRole role = null; * * if(rolesFound.Count == 0) * { * // Hey, I couldn't find this role, Can I make a new one? * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed($"There's no role that is named `{roleName}`, Shall I create it? Y/N").ToEmbed() * }); * * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * * if (msg.Content.ToLower()[0] != 'y') * { * throw new RoleNullException(); * } * * role = await e.Guild.CreateRoleAsync(new CreateRoleArgs * { * Name = roleName, * }); * } * else if (rolesFound.Count > 1) * { * string roleIds = string.Join("\n", rolesFound.Select(x => $"`{x.Name}`: {x.Id}")); * * if (roleIds.Length > 1024) * { * roleIds = roleIds.Substring(0, 1024); * } * * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription("I found multiple roles with that name, which one would you like? please enter the ID") * .AddInlineField("Roles - Ids", roleIds) * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * while(true) * { * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * if (ulong.TryParse(msg.Content, out ulong id)) * { * role = rolesFound.Where(x => x.Id == id) * .FirstOrDefault(); * * if (role != null) * { * break; * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs { * embed = e.ErrorEmbed("I couldn't find that role id in the list. Try again!") * .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() * }); * } * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed("I couldn't find that role. Try again!") * .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() * }); * } * } * } * else * { * role = rolesFound.FirstOrDefault(); * } * * LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); * if(newRole == null) * { * newRole = (await context.LevelRoles.AddAsync(new LevelRole() * { * RoleId = role.Id.ToDbLong(), * GuildId = e.Guild.Id.ToDbLong() * })).Entity; * } * * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription("Is there a role that is needed to get this role? Type out the role name, or `-` to skip") * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * * while (true) * { * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * * rolesFound = (await GetRolesByName(e.Guild, msg.Content.ToLower())); * IDiscordRole parentRole = null; * * if (rolesFound.Count > 1) * { * string roleIds = string.Join("\n", rolesFound.Select(x => $"`{x.Name}`: {x.Id}")); * * if (roleIds.Length > 1024) * { * roleIds = roleIds.Substring(0, 1024); * } * * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription("I found multiple roles with that name, which one would you like? please enter the ID") * .AddInlineField("Roles - Ids", roleIds) * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * while (true) * { * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * if (ulong.TryParse(msg.Content, out ulong id)) * { * parentRole = rolesFound.Where(x => x.Id == id) * .FirstOrDefault(); * * if (parentRole != null) * { * break; * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs { * embed = e.ErrorEmbed("I couldn't find that role id in the list. Try again!") * .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() * }) ; * } * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed("I couldn't find that role. Try again!") * .AddInlineField("Roles - Ids", string.Join("\n", roleIds)).ToEmbed() * }); * } * } * } * else * { * parentRole = rolesFound.FirstOrDefault(); * } * * if (parentRole != null || msg.Content == "-") * { * newRole.RequiredRole = (long?)parentRole?.Id ?? 0; * break; * } * * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed("I couldn't find that role. Try again!").ToEmbed() * }); * } * * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription($"Is there a level requirement? type a number, if there is no requirement type 0") * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * * while (true) * { * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * * if (int.TryParse(msg.Content, out int r)) * { * if (r >= 0) * { * newRole.RequiredLevel = r; * break; * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = sourceEmbed.SetDescription($"Please pick a number above 0. Try again") * .ToEmbed() * }); * } * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = sourceEmbed.SetDescription($"Are you sure `{msg.Content}` is a number? Try again").ToEmbed() * }); * } * } * * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription($"Should I give them when the user level ups? type `yes`, otherwise it will be considered as no") * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * if (msg == null) * { * return; * } * * newRole.Automatic = msg.Content.ToLower()[0] == 'y'; * * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription($"Should users be able to opt in? type `yes`, otherwise it will be considered as no") * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * * newRole.Optable = msg.Content.ToLower()[0] == 'y'; * * if (newRole.Optable) * { * sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") * .SetDescription($"Do you want the user to pay mekos for the role? Enter any amount. Enter 0 to keep the role free.") * .SetColor(138, 182, 239); * * sourceMessage = await sourceEmbed.ToEmbed().SendToChannel(e.Channel); * * while (true) * { * msg = await e.EventSystem.GetCommandHandler<MessageListener>().WaitForNextMessage(e.CreateSession()); * * if (msg == null) * { * return; * } * * if (int.TryParse(msg.Content, out int r)) * { * if (r >= 0) * { * newRole.Price = r; * break; * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed($"Please pick a number above 0. Try again").ToEmbed() * }); * } * } * else * { * await sourceMessage.EditAsync(new EditMessageArgs * { * embed = e.ErrorEmbed($"Not quite sure if this is a number 🤔").ToEmbed() * }); * } * } * } * * await context.SaveChangesAsync(); * Utils.Embed.SetTitle("⚙ Role Config") * .SetColor(102, 117, 127) * .SetDescription($"Updated {role.Name}!") * .ToEmbed().QueueToChannelAsync(e.Channel); * } * }*/ public async Task ConfigRoleQuickAsync(CommandContext e) { var context = e.GetService <MikiDbContext>(); if (!e.Arguments.Take(out string roleName)) { await e.ErrorEmbed("Expected a role name") .ToEmbed().QueueToChannelAsync(e.Channel); return; } IDiscordRole role = null; if (ulong.TryParse(roleName, out ulong s)) { role = await e.Guild.GetRoleAsync(s); } else { role = (await GetRolesByName(e.Guild, roleName)).FirstOrDefault(); } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); MSLResponse arguments = new MMLParser(e.Arguments.Pack.TakeAll()) .Parse(); if (role.Name.Length > 20) { await e.ErrorEmbed("Please keep role names below 20 letters.") .ToEmbed().SendToChannel(e.Channel); return; } if (newRole == null) { newRole = context.LevelRoles.Add(new LevelRole() { GuildId = (e.Guild.Id.ToDbLong()), RoleId = (role.Id.ToDbLong()), }).Entity; } if (arguments.HasKey("automatic")) { newRole.Automatic = arguments.GetBool("automatic"); } if (arguments.HasKey("optable")) { newRole.Optable = arguments.GetBool("optable"); } if (arguments.HasKey("level-required")) { newRole.RequiredLevel = arguments.GetInt("level-required"); } if (arguments.HasKey("price")) { newRole.Price = arguments.GetInt("price"); if (newRole.Price < 0) { throw new ArgumentLessThanZeroException(); } } if (arguments.HasKey("role-required")) { long id = 0; if (arguments.TryGet("role-required", out long l)) { id = l; } else { var r = (await e.Guild.GetRolesAsync()) .Where(x => x.Name.ToLower() == arguments.GetString("role-required").ToLower()) .FirstOrDefault(); if (r != null) { id = r.Id.ToDbLong(); } } if (id != 0) { newRole.RequiredRole = id; } } await context.SaveChangesAsync(); await new EmbedBuilder() .SetTitle("⚙ Role Config") .SetColor(102, 117, 127) .SetDescription($"Updated {role.Name}!") .ToEmbed().QueueToChannelAsync(e.Channel); }
public static async Task <IDiscordRole> GetRoleAsync(this LevelRole role) { return(await Global.Client.Client.GetRoleAsync((ulong)role.GuildId, (ulong)role.RoleId)); }
public async Task IAmAsync(EventContext e) { IDiscordRole role = GetRoleByName(e.Guild, e.arguments); if (role == null) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_null")) .SendToChannel(e.Channel); return; } if (e.Author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_already_given")) .SendToChannel(e.Channel); return; } using (var context = new MikiContext()) { LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); IDiscordUser discordUser = await e.Guild.GetUserAsync(user.Id.FromDbLong()); if (newRole == null || !newRole.Optable) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_forbidden")) .SendToChannel(e.Channel); return; } if (newRole.RequiredLevel > user.Level) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_level_low", newRole.RequiredLevel - user.Level)) .SendToChannel(e.Channel); return; } if (newRole.RequiredRole != 0 && !discordUser.RoleIds.Contains(newRole.RequiredRole.FromDbLong())) { await e.ErrorEmbed(e.Channel.GetLocale().GetString("error_role_required", $"**{e.Guild.GetRole(newRole.RequiredRole.FromDbLong()).Name}**")) .SendToChannel(e.Channel); return; } if (newRole.Price > 0) { if (user.Currency >= newRole.Price) { await e.Channel.SendMessageAsync($"Getting this role costs you {newRole.Price} mekos! type `yes` to proceed."); IDiscordMessage m = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); if (m.Content.ToLower()[0] == 'y') { User serverOwner = await context.Users.FindAsync(e.Guild.OwnerId.ToDbLong()); await user.AddCurrencyAsync(-newRole.Price); await serverOwner.AddCurrencyAsync(newRole.Price); await context.SaveChangesAsync(); } } else { await e.ErrorEmbed(e.Channel.GetLocale().GetString("user_error_insufficient_mekos")) .SendToChannel(e.Channel); return; } } await e.Author.AddRoleAsync(newRole.Role); Utils.Embed.SetTitle("I AM") .SetColor(128, 255, 128) .SetDescription($"You're a(n) {role.Name} now!") .QueueToChannel(e.Channel); } }
public async Task ConfigRoleInteractiveAsync(EventContext e) { using (var context = new MikiContext()) { IDiscordEmbed sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription("Type out the role name you want to config") .SetColor(138, 182, 239); IDiscordMessage sourceMessage = await sourceEmbed.SendToChannel(e.Channel); IDiscordMessage msg = null; while (true) { msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); if (msg.Content.Length < 20) { break; } sourceMessage.Modify("", e.ErrorEmbed("That role name is way too long! Try again.")); } IDiscordRole role = GetRoleByName(e.Guild, msg.Content.ToLower()); LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); if (newRole == null) { newRole = (await context.LevelRoles.AddAsync(new LevelRole() { RoleId = role.Id.ToDbLong(), GuildId = e.Guild.Id.ToDbLong() })).Entity; } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription("Is there a role that is needed to get this role? Type out the role name, or `-` to skip") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.SendToChannel(e.Channel); while (true) { msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); IDiscordRole parentRole = GetRoleByName(e.Guild, msg.Content.ToLower()); if (parentRole != null || msg.Content == "-") { newRole.RequiredRole = (long?)parentRole?.Id ?? 0; break; } sourceMessage.Modify(null, e.ErrorEmbed("I couldn't find that role. Try again!")); } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Is there a level requirement? type a number, if there is no requirement type 0") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.SendToChannel(e.Channel); while (true) { msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); if (int.TryParse(msg.Content, out int r)) { if (r >= 0) { newRole.RequiredLevel = r; break; } else { sourceMessage.Modify(null, sourceEmbed.SetDescription($"Please pick a number above 0. Try again")); } } else { sourceMessage.Modify(null, sourceEmbed.SetDescription($"Are you sure `{msg.Content}` is a number? Try again")); } } sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Should I give them when the user level ups? type `yes`, otherwise it will be considered as no") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.SendToChannel(e.Channel); msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); if (msg == null) { return; } newRole.Automatic = msg.Content.ToLower()[0] == 'y'; sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Should users be able to opt in? type `yes`, otherwise it will be considered as no") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.SendToChannel(e.Channel); msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); newRole.Optable = msg.Content.ToLower()[0] == 'y'; if (newRole.Optable) { sourceEmbed = Utils.Embed.SetTitle("⚙ Interactive Mode") .SetDescription($"Do you want the user to pay mekos for the role? Enter any amount. Enter 0 to keep the role free.") .SetColor(138, 182, 239); sourceMessage = await sourceEmbed.SendToChannel(e.Channel); while (true) { msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); if (msg == null) { return; } if (int.TryParse(msg.Content, out int r)) { if (r >= 0) { newRole.Price = r; break; } else { sourceMessage.Modify(null, e.ErrorEmbed($"Please pick a number above 0. Try again")); } } else { sourceMessage.Modify(null, e.ErrorEmbed($"Not quite sure if this is a number 🤔")); } } } await context.SaveChangesAsync(); Utils.Embed.SetTitle("⚙ Role Config") .SetColor(102, 117, 127) .SetDescription($"Updated {role.Name}!") .QueueToChannel(e.Channel); } }
public static async Task <IDiscordRole> GetRoleAsync(this LevelRole role) { return(await MikiApp.Instance.Discord.GetRoleAsync((ulong)role.GuildId, (ulong)role.RoleId)); }
public async Task IAmAsync(EventContext e) { using (var context = new MikiContext()) { string roleName = e.Arguments.ToString(); List <IDiscordRole> roles = await GetRolesByName(e.Guild, roleName); IDiscordRole role = null; // checking if the role has a duplicate name. if (roles.Count > 1) { List <LevelRole> levelRoles = await context.LevelRoles.Where(x => x.GuildId == (long)e.Guild.Id).ToListAsync(); if (levelRoles.Where(x => x.GetRoleAsync().Result.Name.ToLower() == roleName.ToLower()).Count() > 1) { await e.ErrorEmbed("two roles configured have the same name.") .ToEmbed().QueueToChannelAsync(e.Channel); return; } else { role = levelRoles.Where(x => x.GetRoleAsync().Result.Name.ToLower() == roleName.ToLower()).FirstOrDefault().GetRoleAsync().Result; } } else { role = roles.FirstOrDefault(); } // checking if the role is null if (role == null) { await e.ErrorEmbedResource("error_role_null") .ToEmbed().QueueToChannelAsync(e.Channel); return; } IDiscordGuildUser author = await e.Guild.GetMemberAsync(e.Author.Id); if (author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(e.Locale.GetString("error_role_already_given")) .ToEmbed().QueueToChannelAsync(e.Channel); return; } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); IDiscordRole discordRole = await newRole.GetRoleAsync(); User user = (await context.Users.FindAsync(e.Author.Id.ToDbLong())); IDiscordGuildUser discordUser = await e.Guild.GetMemberAsync(user.Id.FromDbLong()); LocalExperience localUser = await LocalExperience.GetAsync(context, e.Guild.Id.ToDbLong(), discordUser.Id.ToDbLong(), discordUser.Username); if (!newRole?.Optable ?? false) { await e.ErrorEmbed(e.Locale.GetString("error_role_forbidden")) .ToEmbed().SendToChannel(e.Channel); return; } int level = User.CalculateLevel(localUser.Experience); if (newRole.RequiredLevel > level) { await e.ErrorEmbed(e.Locale.GetString("error_role_level_low", newRole.RequiredLevel - level)) .ToEmbed().SendToChannel(e.Channel); return; } if (newRole.RequiredRole != 0 && !discordUser.RoleIds.Contains(newRole.RequiredRole.FromDbLong())) { var requiredRole = await e.Guild.GetRoleAsync(newRole.RequiredRole.FromDbLong()); await e.ErrorEmbed( e.Locale.GetString( "error_role_required", $"**{requiredRole.Name}**" ) ).ToEmbed().QueueToChannelAsync(e.Channel); return; } if (newRole.Price > 0) { if (user.Currency >= newRole.Price) { user.RemoveCurrency(newRole.Price); await context.SaveChangesAsync(); } else { await e.ErrorEmbed(e.Locale.GetString("user_error_insufficient_mekos")) .ToEmbed().SendToChannel(e.Channel); return; } } var me = await e.Guild.GetSelfAsync(); if (!await me.HasPermissionsAsync(GuildPermission.ManageRoles)) { await e.ErrorEmbed(e.Locale.GetString("permission_error_low", "give roles")).ToEmbed() .QueueToChannelAsync(e.Channel); return; } if (discordRole.Position >= await me.GetHierarchyAsync()) { await e.ErrorEmbed(e.Locale.GetString("permission_error_low", "give roles")).ToEmbed() .QueueToChannelAsync(e.Channel); return; } await author.AddRoleAsync(discordRole); await new EmbedBuilder() .SetTitle("I AM") .SetColor(128, 255, 128) .SetDescription($"You're a(n) {role.Name} now!") .ToEmbed().QueueToChannelAsync(e.Channel); } }
public async Task IAmAsync(EventContext e) { using (var context = new MikiContext()) { string roleName = e.Arguments.ToString(); List <IDiscordRole> roles = await GetRolesByName(e.Guild, roleName); IDiscordRole role = null; if (roles.Count > 1) { List <LevelRole> levelRoles = await context.LevelRoles.Where(x => x.GuildId == (long)e.Guild.Id).ToListAsync(); if (levelRoles.Where(x => x.Role.Name.ToLower() == roleName.ToLower()).Count() > 1) { e.ErrorEmbed("two roles configured have the same name.") .ToEmbed().QueueToChannel(e.Channel); return; } else { role = levelRoles.Where(x => x.Role.Name.ToLower() == roleName.ToLower()).FirstOrDefault().Role; } } else { role = roles.FirstOrDefault(); } if (role == null) { e.ErrorEmbedResource("error_role_null") .ToEmbed().QueueToChannel(e.Channel); return; } IDiscordGuildUser author = await e.Guild.GetUserAsync(e.Author.Id); if (author.RoleIds.Contains(role.Id)) { e.ErrorEmbed(e.GetResource("error_role_already_given")) .ToEmbed().QueueToChannel(e.Channel); return; } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); User user = (await context.Users.FindAsync(e.Author.Id.ToDbLong())); IDiscordGuildUser discordUser = await e.Guild.GetUserAsync(user.Id.FromDbLong()); LocalExperience localUser = await LocalExperience.GetAsync(context, e.Guild.Id.ToDbLong(), discordUser); if (!newRole?.Optable ?? false) { await e.ErrorEmbed(e.GetResource("error_role_forbidden")) .ToEmbed().SendToChannel(e.Channel); return; } int level = User.CalculateLevel(localUser.Experience); if (newRole.RequiredLevel > level) { await e.ErrorEmbed(e.GetResource("error_role_level_low", newRole.RequiredLevel - level)) .ToEmbed().SendToChannel(e.Channel); return; } if (newRole.RequiredRole != 0 && !discordUser.RoleIds.Contains(newRole.RequiredRole.FromDbLong())) { await e.ErrorEmbed( e.GetResource( "error_role_required", $"**{(await e.Guild.GetRoleAsync(newRole.RequiredRole.FromDbLong())).Name}**" )).ToEmbed().SendToChannel(e.Channel); return; } if (newRole.Price > 0) { if (user.Currency >= newRole.Price) { await e.Channel.SendMessageAsync($"Getting this role costs you {newRole.Price} mekos! type `yes` to proceed."); IDiscordMessage m = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (m.Content.ToLower()[0] == 'y') { await user.AddCurrencyAsync(-newRole.Price); await context.SaveChangesAsync(); } else { await e.ErrorEmbed("Purchase Cancelled") .ToEmbed().SendToChannel(e.Channel); return; } } else { await e.ErrorEmbed(e.GetResource("user_error_insufficient_mekos")) .ToEmbed().SendToChannel(e.Channel); return; } } await author.AddRoleAsync(newRole.Role); Utils.Embed.SetTitle("I AM") .SetColor(128, 255, 128) .SetDescription($"You're a(n) {role.Name} now!") .ToEmbed().QueueToChannel(e.Channel); } }
public async Task SetRoleLevelAsync(EventContext e) { using (var context = new MikiContext()) { Locale locale = Locale.GetEntity(e.Channel.Id.ToDbLong()); List <string> allArgs = new List <string>(); allArgs.AddRange(e.arguments.Split(' ')); if (allArgs.Count >= 2) { int levelrequirement = int.Parse(allArgs[allArgs.Count - 1]); allArgs.RemoveAt(allArgs.Count - 1); IDiscordRole role = e.Guild.Roles .Find(r => r.Name.ToLower() == string.Join(" ", allArgs).TrimEnd(' ').TrimStart(' ').ToLower()); if (role == null) { await e.ErrorEmbed(e.GetResource("error_role_not_found")) .SendToChannel(e.Channel); return; } LevelRole lr = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); if (lr == null) { lr = context.LevelRoles.Add(new LevelRole() { GuildId = e.Guild.Id.ToDbLong(), RoleId = role.Id.ToDbLong(), RequiredLevel = levelrequirement }); IDiscordEmbed embed = Utils.Embed; embed.Title = "Added Role!"; embed.Description = $"I'll give someone the role {role.Name} when he/she reaches level {levelrequirement}!"; if (!e.CurrentUser.HasPermissions(e.Channel, DiscordGuildPermission.ManageRoles)) { embed.AddInlineField(e.GetResource("miki_warning"), e.GetResource("setrolelevel_error_no_permissions", $"`{e.GetResource("permission_manage_roles")}`")); } await embed.SendToChannel(e.Channel); } else { lr.RequiredLevel = levelrequirement; IDiscordEmbed embed = Utils.Embed; embed.Title = "Updated Role!"; embed.Description = $"I'll give someone the role {role.Name} when he/she reaches level {levelrequirement}!"; await embed.SendToChannel(e.Channel); } await context.SaveChangesAsync(); } else { await Utils.ErrorEmbed(locale, "Make sure to fill out both the role and the level when creating this!") .SendToChannel(e.Channel); } } }
public async Task ConfigRoleQuickAsync(EventContext e) { using (var context = new MikiContext()) { string roleName = e.Arguments.ToString().Split('"')[1]; IDiscordRole role = null; if (ulong.TryParse(roleName, out ulong s)) { role = await e.Guild.GetRoleAsync(s); } else { role = (await GetRolesByName(e.Guild, roleName)).FirstOrDefault(); } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); MSLResponse arguments = new MMLParser(e.Arguments.ToString().Substring(roleName.Length + 3)) .Parse(); if (role.Name.Length > 20) { await e.ErrorEmbed("Please keep role names below 20 letters.") .ToEmbed().SendToChannel(e.Channel); return; } if (newRole == null) { newRole = context.LevelRoles.Add(new LevelRole() { GuildId = (e.Guild.Id.ToDbLong()), RoleId = (role.Id.ToDbLong()), }).Entity; } if (arguments.HasKey("automatic")) { newRole.Automatic = arguments.GetBool("automatic"); } if (arguments.HasKey("optable")) { newRole.Optable = arguments.GetBool("optable"); } if (arguments.HasKey("level-required")) { newRole.RequiredLevel = arguments.GetInt("level-required"); } if (arguments.HasKey("role-required")) { long id = 0; if (arguments.TryGet("role-required", out long l)) { id = l; } else { var r = (await e.Guild.GetRolesAsync()) .Where(x => x.Name.ToLower() == arguments.GetString("role-required").ToLower()) .FirstOrDefault(); if (r != null) { id = r.Id.ToDbLong(); } } if (id != 0) { newRole.RequiredRole = id; } } await context.SaveChangesAsync(); Utils.Embed.SetTitle("⚙ Role Config") .SetColor(102, 117, 127) .SetDescription($"Updated {role.Name}!") .ToEmbed().QueueToChannel(e.Channel); } }
public async Task IAmNotAsync(EventContext e) { string roleName = e.Arguments.ToString(); using (var context = new MikiContext()) { List <IDiscordRole> roles = await GetRolesByName(e.Guild, roleName); IDiscordRole role = null; if (roles.Count > 1) { List <LevelRole> levelRoles = await context.LevelRoles.Where(x => x.GuildId == (long)e.Guild.Id).ToListAsync(); if (levelRoles.Where(x => x.GetRoleAsync().Result.Name.ToLower() == roleName.ToLower()).Count() > 1) { e.ErrorEmbed("two roles configured have the same name.") .ToEmbed().QueueToChannel(e.Channel); return; } else { role = levelRoles.Where(x => x.GetRoleAsync().Result.Name.ToLower() == roleName.ToLower()).FirstOrDefault().GetRoleAsync().Result; } } else { role = roles.FirstOrDefault(); } if (role == null) { await e.ErrorEmbed(e.Locale.GetString("error_role_null")) .ToEmbed().SendToChannel(e.Channel); return; } IDiscordGuildUser author = await e.Guild.GetMemberAsync(e.Author.Id); IDiscordGuildUser me = await e.Guild.GetSelfAsync(); if (!author.RoleIds.Contains(role.Id)) { await e.ErrorEmbed(e.Locale.GetString("error_role_forbidden")) .ToEmbed().SendToChannel(e.Channel); return; } LevelRole newRole = await context.LevelRoles.FindAsync(e.Guild.Id.ToDbLong(), role.Id.ToDbLong()); User user = await context.Users.FindAsync(e.Author.Id.ToDbLong()); if (!await me.HasPermissionsAsync(GuildPermission.ManageRoles)) { e.ErrorEmbed(e.Locale.GetString("permission_error_low", "give roles")).ToEmbed() .QueueToChannel(e.Channel); return; } if ((await newRole.GetRoleAsync()).Position >= await me.GetHierarchyAsync()) { e.ErrorEmbed(e.Locale.GetString("permission_error_low", "give roles")).ToEmbed() .QueueToChannel(e.Channel); return; } await author.RemoveRoleAsync(newRole.GetRoleAsync().Result); new EmbedBuilder() .SetTitle("I AM NOT") .SetColor(255, 128, 128) .SetDescription($"You're no longer a(n) {role.Name}!") .ToEmbed().QueueToChannel(e.Channel); } }