Beispiel #1
0
        public async Task PollCommand([Remainder] string input)
        {
            var chan = Context.Channel as IGuildChannel;
            //await chan.AddPermissionOverwriteAsync(Context.Guild.EveryoneRole, new OverwritePermissions(addReactions: PermValue.Deny));
            List <string> options = new List <string>();
            StringBuilder split   = new StringBuilder();

            foreach (char c in input)
            {
                if (c != '|')
                {
                    split.Append(c);
                }
                if (c == '|')
                {
                    options.Add(split.ToString());
                    split.Clear();
                }
            }
            options.Add(split.ToString());
            if (options.Count == 1)
            {
                return;
            }
            if (options.Count == 2)
            {
                await ReplyAsync("dafuq u trying to do");

                return;
            }
            int count = options.Count - 1;

            if (options.Count >= 21)
            {
                await ReplyAsync("Too many options, max 20");

                return;
            }
            char[] alpha = "abcdefghjiklmnopqrstuvwxyz".ToCharArray();

            var texte = "This is a single vote poll, only the last reaction counts";


            var msg = await ReplyAsync("", false, new EmbedBuilder { Description = "Please wait..." });

            var poll = new Poll
            {
                MessageId  = msg.Id,
                IsMultiple = false,
                Options    = options,
                ChannelId  = msg.Channel.Id,
                GuildId    = (msg.Channel as IGuildChannel).GuildId,
                Votes      = new Dictionary <ulong, string>(),
                Emotes     = new List <string>()
            };

            using (var db = new DatabaseContext())
            {
                db.Polls.Add(poll);
                db.SaveChanges();
            }

            var embed = new EmbedBuilder
            {
                Author = new EmbedAuthorBuilder {
                    Name = Context.User.Username, IconUrl = Context.User.GetAvatarUrl()
                },
                Title       = "Poll #" + poll.Id,
                Description = options.First(),
                Color       = new Color(178, 224, 40),
                Footer      = new EmbedFooterBuilder {
                    IconUrl = Context.Client.CurrentUser.GetAvatarUrl(), Text = texte
                }
            };

            options.Remove(options.First());



            int counter = 0;

            foreach (string s in options)
            {
                embed.AddField(new EmbedFieldBuilder
                {
                    IsInline = true,
                    Name     = EmojiMaker.Get(alpha[counter]),
                    Value    = s,
                });
                counter++;
            }

            embed.WithUrl("http://heeeeeeeey.com/");
            await msg.ModifyAsync(x => x.Embed = embed.Build());

            for (int i = 0; i != count; i++)
            {
                await msg.AddReactionAsync(new Discord.Emoji(EmojiMaker.Get(alpha[i])));

                await Task.Delay(1100);
            }
        }
        public async Task PollCloseCommand(int id)
        {
            Poll currentPoll = null;

            using (var db = new DatabaseContext())
            {
                try { currentPoll = db.Polls.FirstOrDefault(x => x.Id == id); } catch { }
            }

            if (currentPoll == null)
            {
                await ReplyAsync("A poll with that id doesnt exist");

                return;
            }
            if (currentPoll.GuildId != Context.Guild.Id)
            {
                await ReplyAsync("That poll is from a different guild");

                return;
            }
            var      channel = (Context.Guild.GetChannel(currentPoll.ChannelId) as ISocketMessageChannel);
            IMessage message = await channel.GetMessageAsync(currentPoll.MessageId);


            char[] alpha = "abcdefghjiklmnopqrstuvwxyz".ToCharArray();
            Dictionary <string, int> pepe = new Dictionary <string, int>();

            pepe.Clear();



            foreach (var c in currentPoll.Emotes)
            {
                try { pepe.Add(c.ToString(), 0); } catch { }
            }
            if (!currentPoll.IsMultiple)
            {
                foreach (var c in currentPoll.Votes.Values)
                {
                    pepe.TryGetValue(c.ToString(), out int val);
                    val++;
                    pepe.Remove(c.ToString());
                    pepe.Add(c.ToString(), val);
                }
            }
            else
            {
                var cyka = await Context.Channel.GetMessageAsync(currentPoll.MessageId) as IUserMessage;

                foreach (var c in cyka.Reactions)
                {
                    pepe.Add(c.Key.Name, c.Value.ReactionCount - 1);
                }
            }


            var embed = new EmbedBuilder
            {
                Author = new EmbedAuthorBuilder {
                    Name = Context.User.Username, IconUrl = Context.User.GetAvatarUrl()
                },
                Title       = $"Poll #{currentPoll.Id} results",
                Description = message.Embeds.First().Description,
                Color       = new Color(178, 224, 40),
            };



            for (int i = 0; i < pepe.Count; i++)
            {
                var        emoji = EmojiMaker.Get(alpha[i]);
                EmbedField field = message.Embeds.First().Fields.Where(x => x.Name == emoji).First();

                pepe.TryGetValue(emoji, out int value);

                embed.AddInlineField(EmojiMaker.Get(alpha[i]) + field.Value, $"Total votes: {value}");
            }

            using (var db = new DatabaseContext())
            {
                db.Polls.Remove(currentPoll);
                db.SaveChanges();
            }


            embed.WithUrl("http://heeeeeeeey.com/");
            await ReplyAsync("", false, embed);
        }