Esempio n. 1
0
        static void Main(string[] args)
        {
            var cs = SettingsAccessor.GetSetting<string>("DBConnectionString");
            var botAPIKey = SettingsAccessor.GetSetting<string>("SlackBotAPIKey");

            //inital connection to database
            using (var db = new DatabaseContext())
            {
                db.Database.EnsureCreated();
            }

            bot.Aliases = new List<string>() { "beef" };
            bot.Responders.Add(new AddBeefResponder());
            bot.Responders.Add(new CurrentBeefsResponder());
            bot.Connect(botAPIKey);

            Console.CancelKeyPress += delegate
            {
                bot.Disconnect();
                Console.WriteLine("Got signal to shut down.");
                exitMre.Set();
            };

            exitMre.WaitOne();
        }
        public BotMessage GetResponse(ResponseContext context)
        {
            var match = commandPattern.Match(context.Message.Text);

            using (var db = new DatabaseContext())
            {
                var newBeef = new BeefEntry();
                newBeef.ReportedOn = DateTimeOffset.UtcNow;

                var internalMessageUserId = context.Message.User.ID;
                var reportedByUserName = context.UserNameCache[internalMessageUserId];

                newBeef.ReporterUserId = reportedByUserName;
                newBeef.ExpiresOn = DetermineExpirationDate(match);
                newBeef.OffendingChatUserId = match.Groups[4].Value.Parse<int>();
                newBeef.Explination = match.Groups[5].Value;

                db.BeefEntries.Add(newBeef);
                db.SaveChanges();
            }

            return new BotMessage
            {
                Text = "Saved beef entry."
            };
        }
        public BotMessage GetResponse(ResponseContext context)
        {
            using (var db = new DatabaseContext())
            {
                var currentBeefEntries = db.BeefEntries
                    .Where(x => x.ExpiresOn > DateTimeOffset.UtcNow)
                    .OrderBy(x => x.ReportedOn)
                    .ToList();

                var resultsTable = currentBeefEntries
                    .ToStringTable(
                        new[]
                        {
                            "Offending Member",
                            "Reported By",
                            "Reported On",
                            "Expires On"
                        },
                        x => x.OffendingChatUserId,
                        x => x.ReporterUserId,
                        x => x.ReportedOn.ToString("yyyy-MM-dd HH:mm:ss 'UTC'"),
                        x => x.ExpiresOn.ToString("yyyy-MM-dd HH:mm:ss 'UTC'"));

                var outputMessage = $"Current beefs:{Environment.NewLine}```{resultsTable}```";

                return new BotMessage()
                {
                    Text = outputMessage
                };
            }
        }
        public BotMessage GetResponse(ResponseContext context)
        {
            using (var db = new DatabaseContext())
            {
                var currentBeefEntries = db.BeefEntries
                    .Where(x => x.ExpiresOn > DateTimeOffset.UtcNow)
                    .OrderBy(x => x.ReportedOn)
                    .ToList();

                var resultsTable = currentBeefEntries
                    .ToStringTable(
                        new[]
                        {
                            "Id",
                            "Offender",
                            "Reported By",
                            "Reported On",
                            "Expires On",
                            "Explanation"
                        },
                        x => x.Id,
                        x => $"{SOChatAccessor.GetUserNameForChatId(x.OffendingChatUserId)} ({x.OffendingChatUserId})",
                        x => x.ReporterUserId,
                        x => x.ReportedOn.ToString("yyyy-MM-dd HH:mm 'UTC'"),
                        x => x.ExpiresOn.ToString("yyyy-MM-dd HH:mm 'UTC'"),
                        x => x.ShortExplanation);

                var outputMessage = $"Current beefs:{Environment.NewLine}```{resultsTable}```";

                return new BotMessage()
                {
                    Text = outputMessage
                };
            }
        }
        private static void InitializeDatabase()
        {
            Console.WriteLine("Setting up database.");

            //initial connection to database
            using (var db = new DatabaseContext())
            {
                bool dbSetUp = false;

                //loop until the connection works
                while (!dbSetUp)
                {
                    try
                    {
                        //create the database if it does not exist and push and new migrations to it
                        db.Database.Migrate();
                        dbSetUp = true;
                    }
                    catch (SocketException ex)
                    {
                        Console.WriteLine("Caught error when trying to set up database. Waiting 30 seconds to retry.");
                        Console.WriteLine(ex.Message);
                        Thread.Sleep(30 * 1000);
                    }
                }
            }
        }
        public override BotMessage GetResponse(ResponseContext context)
        {
            var offendingUserId = commandPattern.Match(context.Message.Text).Groups[1].Value.Parse<int>();

            using (var db = new DatabaseContext())
            {
                var dbEntriesForUser = db.BeefEntries
                    .Where(x => x.OffendingChatUserId == offendingUserId)
                    .Where(x => x.ExpiresOn > DateTimeOffset.UtcNow) //only get ones that are active
                    .OrderBy(x => x.ReportedOn)
                    .ToList();

                string outputMessage = "";

                if (!dbEntriesForUser.Any())
                {
                    outputMessage = "This user has no active beefs.";
                }
                else
                {
                    foreach (var dbEntry in dbEntriesForUser)
                    {
                        outputMessage += $"Closing beef #{dbEntry.Id}.\n";
                        dbEntry.ExpiresOn = DateTimeOffset.UtcNow;
                    }

                    db.SaveChanges();
                }

                return new BotMessage
                {
                    Text = outputMessage
                };
            }
        }
        public override BotMessage GetResponse(ResponseContext context)
        {
            var offenderUserId = commandPattern.Match(context.Message.Text).Groups[1].Value.Parse<int>();

            using (var db = new DatabaseContext())
            {
                var dbEntries = db.BeefEntries
                    .Where(x => x.OffendingChatUserId == offenderUserId)
                    .OrderBy(x => x.ReportedOn)
                    .ToList();

                var openBeefsCount = dbEntries
                    .Where(x => x.ExpiresOn >= DateTimeOffset.UtcNow)
                    .Count();

                var headerLine = $"There are currently {dbEntries.Count} beefs recorded against {SOChatAccessor.GetUserNameForChatId(offenderUserId)} ({offenderUserId}), {openBeefsCount} of which are active.";

                string resultsTable = "";

                if (dbEntries.Any())
                {
                    resultsTable = dbEntries
                        .ToStringTable(
                            new[]
                            {
                                "Id",
                                "Reported By",
                                "Reported On",
                                "Expires On",
                                "Explanation"
                            },
                            x => x.Id,
                            x => x.ReporterUserId,
                            x => x.ReportedOn.ToString("yyyy-MM-dd HH:mm 'UTC'"),
                            x => x.ExpiresOn.ToString("yyyy-MM-dd HH:mm 'UTC'"),
                            x => x.ShortExplanation);

                    resultsTable = $"```{resultsTable}```";
                }

                var outputMessage = $"{headerLine}\n{resultsTable}";

                return new BotMessage
                {
                    Text = outputMessage
                };
            }
        }
        public override BotMessage GetResponse(ResponseContext context)
        {
            var daysOffset = commandPattern.Match(context.Message.Text).Groups[1].Value.Parse<int>();
            var minimumDateTime = DateTimeOffset.UtcNow.AddDays(-daysOffset);

            using (var db = new DatabaseContext())
            {
                var dbEntries = db.BeefEntries
                    .Where(x => x.ReportedOn >= minimumDateTime)
                    .OrderBy(x => x.ReportedOn)
                    .ToList();

                var headerLine = $"In the past {daysOffset} days, {dbEntries.Count} beefs have been recorded.";

                string resultsTable = "";

                if (dbEntries.Any())
                {
                    resultsTable = dbEntries
                        .ToStringTable(
                            new[]
                            {
                                "Id",
                                "Offender",
                                "Reported By",
                                "Reported On",
                                "Expires On",
                                "Explanation"
                            },
                            x => x.Id,
                            x => $"{SOChatAccessor.GetUserNameForChatId(x.OffendingChatUserId)} ({x.OffendingChatUserId})",
                            x => x.ReporterUserId,
                            x => x.ReportedOn.ToStandardDisplayString(),
                            x => x.ExpiresOn.ToStandardDisplayString(),
                            x => x.ShortExplanation);

                    resultsTable = $"```{resultsTable}```";
                }

                var outputMessage = $"{headerLine}\n{resultsTable}";

                return new BotMessage
                {
                    Text = outputMessage
                };
            }
        }
        public override BotMessage GetResponse(ResponseContext context)
        {
            var beefIds = commandPattern.Match(context.Message.Text)
                 .Groups[1]
                 .Captures
                 .OfType<Capture>()
                 .Select(x => x.Value.Parse<int>())
                 .ToList();

            using (var db = new DatabaseContext())
            {
                var dbEntries = (from dbRecord in db.BeefEntries
                                 join beefId in beefIds on dbRecord.Id equals beefId
                                 select dbRecord)
                                 .OrderBy(x => x.ReportedOn)
                                 .ToList();

                var outputMessage = "";

                if (dbEntries.Count != beefIds.Count)
                {
                    outputMessage = "*Warning, could not locate all requested entries. Ensure you have typed the Id numbers correctly.*\n\n";
                }

                foreach (var dbEntry in dbEntries)
                {
                    if (dbEntry.ExpiresOn < DateTimeOffset.UtcNow)
                    {
                        //the beef has already expired
                        outputMessage += $"Beef #{dbEntry.Id} already expired on {dbEntry.ExpiresOn.ToStandardDisplayString()}.\n";
                    }
                    else
                    {
                        outputMessage += $"Beef #{dbEntry.Id} closed.\n";
                        dbEntry.ExpiresOn = DateTimeOffset.UtcNow;
                    }
                }

                db.SaveChanges();

                return new BotMessage
                {
                    Text = outputMessage
                };
            }
        }
        public override BotMessage GetResponse(ResponseContext context)
        {
            var beefIds = commandPattern.Match(context.Message.Text)
                .Groups[1]
                .Captures
                .OfType<Capture>()
                .Select(x => x.Value.Parse<int>())
                .ToList();

            using (var db = new DatabaseContext())
            {
                var dbEntries = (from dbRecord in db.BeefEntries
                                 join beefId in beefIds on dbRecord.Id equals beefId
                                 select dbRecord)
                                 .OrderBy(x => x.ReportedOn)
                                 .ToList();

                var outputMessage = "";

                if (dbEntries.Count != beefIds.Count)
                {
                    outputMessage = "*Warning, could not locate all requested entries. Ensure you have typed the Id numbers correctly.*\n\n";
                }

                foreach (var dbEntry in dbEntries)
                {
                    var header = $"*Id #{dbEntry.Id}*.";
                    var details = $"Reported by {dbEntry.ReporterUserId} on {dbEntry.ReportedOn.ToStandardDisplayString()} against {SOChatAccessor.GetUserNameForChatId(dbEntry.OffendingChatUserId)} ({dbEntry.OffendingChatUserId}). {(dbEntry.HasExpired ? "Expired" : "Expires")} {dbEntry.ExpiresOn.ToStandardDisplayString()}.";
                    var explanation = $"> {dbEntry.Explanation}";

                    outputMessage += $"{header} {details}\n{explanation}\n";
                }

                return new BotMessage
                {
                    Text = outputMessage
                };
            }
        }