Ejemplo n.º 1
0
        /// <summary>
        /// 1. LoadDatabase
        /// 2. Append Data
        /// 3. Save Database
        /// </summary>
        /// <returns></returns>
        public async Task AddEntryToDatabase(PollInfo pollInfo)
        {
            var db = await ReadDatabase();

            db.Add(pollInfo);

            var filesFolder = _configuration["FileStore:DataFolder"];

            if (!Directory.Exists(filesFolder))
            {
                _logger.LogError("FileStore:DataFolder does not refer to an existing directory. Did you configure it in appsettings.json?");
                await ReplyAsync("Bot not properly configured. Check logs for details.");

                return;
            }

            var pollModuleDirectory = Directory.CreateDirectory(Path.Combine(filesFolder, "PollModule"));
            var serialized          = JsonConvert.SerializeObject(db);
            await File.WriteAllTextAsync(Path.Combine(pollModuleDirectory.FullName, "PollDb.json"), serialized);
        }
Ejemplo n.º 2
0
        public async Task CreatePollAsync(string title, params string[] options)
        {
            if (options.Length > 10)
            {
                var msg = await ReplyAsync("Das Maximum an 10 Optionen von `poll` wurde überschritten!");

                await Task.Delay(3000);

                await base.Context.Message.DeleteAsync();

                await msg.DeleteAsync();

                return;
            }
            var emojis = new[]
            {
                "one",
                "two",
                "three",
                "four",
                "five",
                "six",
                "seven",
                "eight",
                "nine",
                "keycap_ten"
            }
            .Select(emoji => GEmojiSharp.Emoji.Get(emoji).Raw)
            .ToList();

            var pollInfo = new PollInfo()
            {
                UserId  = base.Context.User.Id,
                Title   = title,
                IsEnded = false
            };

            for (int i = 0; i < options.Length; i++)
            {
                pollInfo.Options.Add(new OptionEmojiPair
                {
                    Emoji  = emojis[i],
                    Option = options[i]
                });
            }

            var builder = new EmbedBuilder()
                          .WithColor(Color.DarkRed)
                          .WithFooter(footer => footer.Text = $"{_prefix}poll")
                          .WithCurrentTimestamp()
                          .WithTitle(title);

            StringBuilder desc = new StringBuilder();

            foreach (var emojiPair in pollInfo.Options)
            {
                desc.AppendLine($"{emojiPair.Emoji} - {emojiPair.Option}\n");
            }

            builder.AddField("Reagiere mit einem Emoji um zu voten:", desc.ToString());

            //await base.Context.Message.DeleteAsync();
            IUserMessage message = await base.Context.Channel.SendMessageAsync(embed : builder.Build());

            pollInfo.MessageId = message.Id;
            pollInfo.ChannelId = message.Channel.Id;

            for (int i = 0; i < options.Length; i++)
            {
                await message.AddReactionAsync(new Emoji(emojis[i]));
            }

            //Save database
            await AddEntryToDatabase(pollInfo);
        }
Ejemplo n.º 3
0
 private Task UpdateDb(PollInfo info) => _container.Upsert(info);
Ejemplo n.º 4
0
 /// <summary>
 /// 1. LoadDatabase
 /// 2. Append Data
 /// 3. Save Database
 /// </summary>
 /// <returns></returns>
 private Task AddEntryToDatabase(PollInfo info) => _container.Insert(info);