示例#1
0
        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;
            }
        }