Пример #1
0
        public async Task CreateGuild(CommandContext e)
        {
            try
            {
                if (Global.InGame)
                {
                    var embed = new DiscordEmbedBuilder
                    {
                        Color = Color.InfoColor,
                        Title = Texts.Errors.AnotherGameIsPlaying
                    };
                    await e.Channel.SendMessageAsync(embed : embed.Build());
                }
                else
                {
                    try
                    {
                        Global.Client = e.Client;


                        var msgs = (await e.Guild.GetDefaultChannel().GetMessagesAsync(10)).ToList()
                                   .FindAll(m => m.Author == e.Client.CurrentUser || m.Content.Contains("!go"));
                        if (msgs.Count > 0)
                        {
                            await e.Guild.GetDefaultChannel().DeleteMessagesAsync(msgs);
                        }
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception);
                    }

                    Console.WriteLine(File.ReadAllText(@"..//Locale/fr/lang.json", Encoding.UTF8));
                    while (Guild == null)
                    {
                        try
                        {
                            Guild   = e.Client.CreateGuildAsync("Loup Garou").GetAwaiter().GetResult();
                            GuildId = Guild.Id;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                    }

                    await Guild.ModifyAsync(x => x.SystemChannel = new Optional <DiscordChannel>(null));

                    await Guild.ModifyAsync(opt =>
                                            opt.DefaultMessageNotifications = DefaultMessageNotifications.MentionsOnly);

                    Global.InGame = true;

                    WriteDebug("1");


                    await GameBuilder.CreateDiscordRoles(); // Role Admin, Joueur, Spectateur

                    WriteDebug("2");
                    await Global.Client.CurrentUser.GetMember().GrantRoleAsync(Global.Roles[PublicRole.Admin]);

                    WriteDebug("3");
                    await(await Guild.GetAllMembersAsync()).First()
                    .ModifyAsync(m => m.Nickname = Texts.DiscordRoles.BotName);
                    WriteDebug("4");
                    Console.WriteLine("Guild Created");
                    DiscordChannels = new Dictionary <GameChannel, DiscordChannel>();

                    await e.TriggerTypingAsync();

                    var generalChannel = Guild.GetDefaultChannel();
                    await generalChannel.ModifyAsync(x => x.Name = "Bot");

                    DiscordChannels.Add(GameChannel.BotText, generalChannel);

                    var botVChannel = await Guild.CreateChannelAsync("Bot", ChannelType.Voice, generalChannel.Parent);

                    DiscordChannels.Add(GameChannel.BotVoice, botVChannel);
                    Global.Client.GuildMemberAdded += NewGuildMember;
                    Global.Client.GuildMemberAdded += StartMember;


                    var inv = await generalChannel.CreateInviteAsync();

                    var msgInv = await e.RespondAsync(inv.ToString());

                    var embed = new DiscordEmbedBuilder
                    {
                        Title = Texts.Annoucement.BotWantPlay,
                        Color = Color.PollColor
                    };
                    await generalChannel.SendMessageAsync(embed : embed.Build());

                    var players = new List <DiscordMember>();


                    await Task.Delay(Global.Config.JoinTime);

                    foreach (var usr in botVChannel.Users)
                    {
                        await usr.RevokeRoleAsync(Global.Roles[PublicRole.Spectator]);

                        await usr.GrantRoleAsync(Global.Roles[PublicRole.Player]);

                        players.Add(usr);
                        WriteDebug(usr.Username);
                    }

                    Global.Client.GuildMemberAdded     -= StartMember;
                    Global.Client.MessageReactionAdded += Listeners.PreventMultiVote;
                    Global.Client.MessageReactionAdded += Listeners.PreventSpectatorFromVote;


                    Console.WriteLine(13);
                    try
                    {
                        GameBuilder.Debug(this);
                        var channelsToRemove = new List <DiscordChannel>();
                        while (Guild.Channels.Count != DiscordChannels.Count)
                        {
                            foreach (var c in Guild.Channels)
                            {
                                try
                                {
                                    if (!DiscordChannels.ContainsValue(c))
                                    {
                                        channelsToRemove.Add(c);
                                    }
                                }
                                catch (NotFoundException exception)
                                {
                                    Console.WriteLine(exception.JsonMessage);
                                }
                            }

                            foreach (var dm in channelsToRemove)
                            {
                                await dm.DeleteAsync();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }


                    Console.WriteLine("Supr fini");

                    await RoleAssignment(msgInv, e, players);

                    foreach (var p in PersonnagesList)
                    {
                        WriteDebug($"Y : {p.Me.Username}");

                        var usr = GameBuilder.GetMember(Guild, p.Me);

                        await DiscordChannels[GameChannel.BotVoice].AddOverwriteAsync(usr, Permissions.None,
                                                                                      GameBuilder.CreatePerms(Permissions.AccessChannels, Permissions.UseVoice));
                    }


                    if (PersonnagesList.Count < 2)
                    {
                        Victory = Victory.NotPlayable;
                        embed   = new DiscordEmbedBuilder
                        {
                            Title = $"{Texts.Errors.NotEnoughPlayer} {PersonnagesList.Count}",
                            Color = Color.InfoColor
                        };
                        await DiscordChannels[GameChannel.TownText].SendMessageAsync(embed: embed.Build());
                        await Task.Delay(Global.Config.JoinTime);

                        await Global.Game.Guild.DeleteAsync();
                    }
                    else
                    {
                        e.Client.VoiceStateUpdated += Listeners.KillLeaver;
                    }

                    while (Victory == Victory.None && Victory != Victory.NotPlayable)
                    {
                        await PlayAsync();
                    }
                }
            }
            catch (Exception e4)
            {
                Console.WriteLine(e4);
            }
        }
Пример #2
0
        public async Task CreateGuild(CommandContext e, string lang = "fr")
        {
            try
            {
                Global.Client = e.Client;
                SetLanguage(lang);

                var msgs = (await e.Guild.GetDefaultChannel().GetMessagesAsync(10)).ToList()
                           .FindAll(m => m.Author == e.Client.CurrentUser || m.Content.Contains("!go"));
                if (msgs.Count > 0)
                {
                    await e.Guild.GetDefaultChannel().DeleteMessagesAsync(msgs);
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }


            while (Guild == null)
            {
                try
                {
                    Guild   = e.Client.CreateGuildAsync("Loup Garou").GetAwaiter().GetResult();
                    GuildId = Guild.Id;
                    await Guild.ModifyAsync(x => x.SystemChannel = null);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            await GameBuilder.CreateDiscordRoles(this);

            await GameBuilder.GetMember(Guild, Global.Client.CurrentUser).GrantRoleAsync(Global.Roles[CustomRoles.Admin]);

            await(await Guild.GetAllMembersAsync()).First().ModifyAsync(m => m.Nickname = Texts.BotName);

            Console.WriteLine("Guild Created");

            DiscordChannels = new Dictionary <GameChannel, DiscordChannel>();

            Console.WriteLine("Delatation faite");

            await e.TriggerTypingAsync();

            var generalChannel = Guild.GetDefaultChannel();
            await generalChannel.ModifyAsync(x => x.Name = "Bot");

            DiscordChannels.Add(GameChannel.BotText, generalChannel);

            var botVChannel = await Guild.CreateChannelAsync("Bot", ChannelType.Voice, generalChannel.Parent);

            DiscordChannels.Add(GameChannel.BotVoice, botVChannel);
            e.Client.GuildMemberAdded += NewGuildMember;
            e.Client.GuildMemberAdded += StartMember;


            var inv = await generalChannel.CreateInviteAsync();

            var msgInv = await e.RespondAsync(inv.ToString());

            var embed = new DiscordEmbedBuilder
            {
                Title = Texts.BotWantPlay,
                Color = Color.PollColor
            };
            var askMessage = await generalChannel.SendMessageAsync(embed : embed.Build());

            var emoji = DiscordEmoji.FromName(e.Client, ":thumbsup:");
            await askMessage.CreateReactionAsync(emoji);


            var players = new List <DiscordMember>();


            try
            {
                var timeToJoin = 10;
                await Task.Delay(timeToJoin * 1000);

                var users = await(await Guild.GetDefaultChannel().GetMessageAsync(askMessage.Id))
                            .GetReactionsAsync(emoji);

                foreach (var usr in users)
                {
                    if (!usr.IsBot)
                    {
                        var dm = await Guild.GetMemberAsync(usr.Id);

                        await dm.RevokeRoleAsync(Global.Roles[CustomRoles.Spectator]);

                        await dm.GrantRoleAsync(Global.Roles[CustomRoles.Player]);

                        players.Add(dm);
                    }
                }

                // DEBUG
                foreach (var discordMember in players)
                {
                    WriteDebug($"Il y a {discordMember.Username} dans le jeu");
                }

                e.Client.GuildMemberAdded -= StartMember;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }


            Console.WriteLine(13);
            try
            {
                GameBuilder.Debug(this);
                var channelsToRemove = new List <DiscordChannel>();
                while (Guild.Channels.Count != DiscordChannels.Count)
                {
                    foreach (var c in Guild.Channels)
                    {
                        try
                        {
                            if (!DiscordChannels.ContainsValue(c))
                            {
                                channelsToRemove.Add(c);
                            }
                        }
                        catch (NotFoundException exception)
                        {
                            Console.WriteLine(exception.JsonMessage);
                        }
                    }

                    foreach (var dm in channelsToRemove)
                    {
                        await dm.DeleteAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }


            Console.WriteLine("Supr fini");

            await RoleAssignment(msgInv, e, players);

            foreach (var p in PersonnagesList)
            {
                WriteDebug($"Y : {p.Me.Username}");

                DiscordMember usr = GameBuilder.GetMember(Guild, p.Me);

                await DiscordChannels[GameChannel.BotVoice].AddOverwriteAsync(usr, Permissions.None,
                                                                              GameBuilder.CreatePerms(Permissions.AccessChannels, Permissions.UseVoice));
            }


            if (PersonnagesList.Count < 2)
            {
                Victory = Victory.NotPlayable;
                embed   = new DiscordEmbedBuilder
                {
                    Title = $"{Texts.NotEnoughPlayer} {PersonnagesList.Count}",
                    Color = Color.InfoColor
                };
                await DiscordChannels[GameChannel.TownText].SendMessageAsync(embed: embed.Build());
            }

            while (Victory == Victory.None && Victory != Victory.NotPlayable)
            {
                await PlayAsync();
            }
        }