/// <summary> /// Retrieves role socket entities from the IDs in the <see cref="Role"/> enum. /// </summary> /// <exception cref="InvalidOperationException">Thrown when a role can't be found.</exception> private async Task GetRoles() { SocketGuild guild = _client.Guilds.FirstOrDefault(); PlayTesterRole = GetRole(Role.Playtester); ModRole = GetRole(Role.Moderators); RconRole = GetRole(Role.RconAccess); MuteRole = GetRole(Role.Muted); ActiveRole = GetRole(Role.ActiveMember); PatreonsRole = GetRole(Role.Patreons); CommunityTesterRole = GetRole(Role.CommunityTester); var roleMeRoles = new HashSet <SocketRole>(); foreach (string role in Config["roleMeWhiteListCSV"].Split(',')) { roleMeRoles.Add(await ParseRole(role)); } RoleMeWhiteList = roleMeRoles.ToImmutableHashSet(); SocketRole GetRole(Role role) { SocketRole r = guild?.GetRole((ulong)role); if (r == null) { throw new InvalidOperationException($"The role '{role}' could not be found."); } return(r); } async Task <SocketRole> ParseRole(string name) { SocketRole role = await RoleTypeReader <SocketRole> .GetBestResultAsync(guild, name); if (role == null) { throw new InvalidOperationException($"'{name}' could not be parsed as a role."); } return(role); } }
public async Task Setup() { var roleTypeReader = new RoleTypeReader <IRole>(); IRole domRole = null; IRole[] flightRoles = new IRole[11]; var embedBuilder = new EmbedBuilder() .WithTitle("Auto-dominance setup") .WithDescription("Welcome to the setup for auto-dominance role!\r\nThis small setup will guide you in setting up all the needed information for me to automagically assign your dominance role to this week's domninance winners.\r\nEvery question has a 30 second time limit to respond, so make sure you have all roles ready.\r\n\nYou can always reply `Stop` to stop the setup.\r\n\nTo begin, what is the role for **Dominance**?"); var interactiveMessage = await ReplyAsync(embed : embedBuilder.Build()); Task StopSetup() { embedBuilder.Fields.RemoveAll(x => true); embedBuilder.Description = $"Setup has been cancelled."; return(interactiveMessage.ModifyAsync(x => x.Embed = embedBuilder.Build())); } while (true) { var roleMessage = await NextMessageAsync(true, true, TimeSpan.FromSeconds(30)); if (roleMessage == null || roleMessage.Content.ToLower().Trim() == "stop") { await StopSetup(); return; } var roleResult = await roleTypeReader.ReadAsync(Context, roleMessage.Content, _serviceProvider); if (roleResult.IsSuccess) { if (Context.Guild.CurrentUser.GuildPermissions.ManageMessages) { await roleMessage.DeleteAsync(); } domRole = (IRole)roleResult.BestMatch; break; } else { embedBuilder.Description = $"I could not find a role with the following input: `{roleMessage.Content}`.\r\n\nPlease try again, you can provide a mention (`@role`) name (`role`) or the id of the role (`000000000000000000`).\r\nWhat is the role for **Dominance**?"; if (Context.Guild.CurrentUser.GuildPermissions.ManageMessages) { await roleMessage.DeleteAsync(); } await interactiveMessage.ModifyAsync(x => x.Embed = embedBuilder.Build()); } } embedBuilder.AddField("Dominance", domRole.Mention); embedBuilder.Description = $"Great! I've found the role {domRole.Mention} and will set that as the role used for **Dominance** once this setup is finished.\r\n\nNext up are the flight roles. What is the role for **{Flight.Earth}**?"; await interactiveMessage.ModifyAsync(x => x.Embed = embedBuilder.Build()); for (var i = 0; i <= 10; i++) { if (flightRoles.Any(x => x != null)) { embedBuilder.Description = $"Great! I've found the role {flightRoles[i - 1].Mention} and will set that as the role used for **{(Flight)i}** once this setup is finished.\r\n\nNext one! What is the role for **{(Flight)i}**?"; await interactiveMessage.ModifyAsync(x => x.Embed = embedBuilder.Build()); } while (true) { var roleMessage = await NextMessageAsync(true, true, TimeSpan.FromSeconds(30)); if (roleMessage == null || roleMessage.Content.ToLower().Trim() == "stop") { await StopSetup(); return; } var roleResult = await roleTypeReader.ReadAsync(Context, roleMessage.Content, _serviceProvider); if (roleResult.IsSuccess) { if (Context.Guild.CurrentUser.GuildPermissions.ManageMessages) { await roleMessage.DeleteAsync(); } flightRoles[i] = (IRole)roleResult.BestMatch; if (embedBuilder.Fields.Any(x => x.Name == "Flights")) { embedBuilder.Fields[1].Value = string.Join("\n", flightRoles.Where(x => x != null).Select(x => x.Mention)); } else { embedBuilder.AddField("Flights", string.Join("\n", flightRoles.Where(x => x != null).Select(x => x.Mention))); } break; } else { embedBuilder.Description = $"I could not find a role with the following input: `{roleMessage.Content}`.\r\n\nPlease try again, you can provide a mention (`@role`) name (`role`) or the id of the role (`000000000000000000`).\r\nWhat is the role for **{(Flight)i}**?"; if (Context.Guild.CurrentUser.GuildPermissions.ManageMessages) { await roleMessage.DeleteAsync(); } await interactiveMessage.ModifyAsync(x => x.Embed = embedBuilder.Build()); } } } if (domRole != null && flightRoles.All(x => x != null)) { embedBuilder.Description = $"Great! I've found the role {flightRoles[10].Mention} and will set that as the role used for **{(Flight.Fire)}**.\r\n\nThat was the end of the setup, everything is now saved and when dominance rolls over on Flight Rising I will update your users' roles!\r\n\nMake sure to have people opt in to this using `{await SettingManager.GetSettingValue("GUILDCONFIG_PREFIX", Context.Guild)}dom optin`!"; await interactiveMessage.ModifyAsync(x => x.Embed = embedBuilder.Build()); SettingManager.SetSettingValue("GUILDCONFIG_DOMINANCE", "true", Context.Guild); SettingManager.SetSettingValue("GUILDCONFIG_DOMINANCE_ROLE", domRole.Id.ToString(), Context.Guild); for (var i = 0; i < 11; i++) { SettingManager.SetSettingValue($"GUILDCONFIG_DOMINANCE_ROLE_{i}", flightRoles[i].Id.ToString(), Context.Guild); } } else { await StopSetup(); return; } }
public async Task InternalReactionRoles(bool exclusive, params string[] input) { var msgs = await((SocketTextChannel)ctx.Channel).GetMessagesAsync().FlattenAsync().ConfigureAwait(false); var prev = (IUserMessage)msgs.FirstOrDefault(x => x is IUserMessage && x.Id != ctx.Message.Id); if (prev == null) { return; } if (input.Length % 2 != 0) { return; } var grp = 0; var results = input .GroupBy(x => grp++ / 2) .Select(async x => { var inputRoleStr = x.First(); var roleReader = new RoleTypeReader <SocketRole>(); var roleResult = await roleReader.ReadAsync(ctx, inputRoleStr, _services); if (!roleResult.IsSuccess) { Log.Warning("Role {0} not found.", inputRoleStr); return(null); } var role = (IRole)roleResult.BestMatch; if (role.Position > ((IGuildUser)ctx.User).GetRoles().Select(r => r.Position).Max() && ctx.User.Id != ctx.Guild.OwnerId) { return(null); } var emote = x.Last().ToIEmote(); return(new { role, emote }); }) .Where(x => x != null); var all = await Task.WhenAll(results); if (!all.Any()) { return; } foreach (var x in all) { try { await prev.AddReactionAsync(x.emote, new RequestOptions() { RetryMode = RetryMode.Retry502 | RetryMode.RetryRatelimit }).ConfigureAwait(false); } catch (Discord.Net.HttpException ex) when(ex.HttpCode == HttpStatusCode.BadRequest) { await ReplyErrorLocalizedAsync("reaction_cant_access", Format.Code(x.emote.ToString())); return; } await Task.Delay(500).ConfigureAwait(false); } if (_service.Add(ctx.Guild.Id, new ReactionRoleMessage() { Exclusive = exclusive, MessageId = prev.Id, ChannelId = prev.Channel.Id, ReactionRoles = all.Select(x => { return(new ReactionRole() { EmoteName = x.emote.ToString(), RoleId = x.role.Id, }); }).ToList(), })) { await ctx.OkAsync(); } else { await ReplyErrorLocalizedAsync("reaction_roles_full").ConfigureAwait(false); } }