public async Task <PockyUser> AddOrUpdateUserAsync(string userId, string username)
        {
            var existingUser = _context.PockyUsers
                               .Include(x => x.PegsGiven)
                               .Include(x => x.Roles)
                               .SingleOrDefault(x => x.UserId == userId);

            if (existingUser != null)
            {
                existingUser.Username = username;
                await _context.SaveChangesAsync();

                return(existingUser);
            }

            PockyUser newPockyUser = new PockyUser
            {
                UserId   = userId,
                Username = username
            };

            _context.Add(newPockyUser);
            await _context.SaveChangesAsync();

            return(newPockyUser);
        }
Exemple #2
0
        private string CreateHelpResponseMessage(string command, PockyUser user)
        {
            if (string.IsNullOrEmpty(command))
            {
                return(CreateCommandListMessage(user));
            }
            switch (command.ToLower(CultureInfo.InvariantCulture))
            {
            case Commands.Peg:
                return(CreatePegHelpMessage());

            case Commands.Status:
                return(CreateStatusHelpMessage());

            case Commands.Keywords:
                return(CreateKeywordsHelpMessage());

            case Commands.Ping:
                return(CreatePingHelpMessage());

            case Commands.Welcome:
                return(CreateWelcomeHelpMessage());

            case Commands.Rotation:
                return(CreateRotationHelpMessage());

            case Commands.Results:
                return(CreateResultsHelpMessage(user));

            case Commands.Reset:
                return(CreateResetHelpMessage(user));

            case Commands.Finish:
                return(CreateFinishHelpMessage(user));

            case Commands.NumberConfig:
                return(CreateNumberConfigHelpMessage(user));

            case Commands.StringConfig:
                return(CreateStringConfigHelpMessage(user));

            case Commands.RoleConfig:
                return(CreateRoleConfigHelpMessage(user));

            case Commands.LocationConfig:
                return(CreateLocationConfigHelpMessage(user));

            case Commands.UserLocation:
                return(CreateUserLocationHelpMessage(user));

            case Commands.LocationWeight:
                return(CreateLocationWeightHelpMessage(user));

            case Commands.RemoveUser:
                return(CreateRemoveUserHelpMessage(user));

            default:
                return(CreateDefaultHelpMessage());
            }
        }
Exemple #3
0
 private async Task PmReceiver(string comment, PockyUser sender, PockyUser receiver)
 {
     await _chatHelper.Messages.SendMessageAsync(new Message
     {
         Text   = $"You have received a new peg from {sender.Username} with message: \"{comment}\".",
         RoomId = receiver.UserId
     }).ConfigureAwait(false);
 }
Exemple #4
0
 internal void TestRespond(Message message, PockyUser user, string[] locations, Message response)
 {
     this.Given(x => GivenAMessage(message))
     .And(x => GivenAUser(user))
     .And(x => GivenLocations(locations))
     .When(x => WhenRespondingToAMessage())
     .Then(x => ThenItShouldReturnAResponse(response))
     .BDDfy();
 }
Exemple #5
0
 private async Task PmSender(PockyUser sender, PockyUser receiver, int numPegsGiven)
 {
     var pegs = numPegsGiven == 1 ? "peg" : "pegs";
     await _chatHelper.Messages.SendMessageAsync(new Message
     {
         Text   = $"Peg given to {receiver.Username}. You have given {numPegsGiven} {pegs} this fortnight.",
         RoomId = sender.UserId
     }).ConfigureAwait(false);
 }
        public async Task RemoveUserAsync(PockyUser user)
        {
            var userLocations = _context.UserLocations.Where(x => x.UserId == user.UserId);

            _context.UserLocations.RemoveRange(userLocations);

            _context.PockyUsers.Remove(user);
            await _context.SaveChangesAsync();
        }
Exemple #7
0
        private static bool UserHasOutstandingPegs(PockyUser user)
        {
            if (user == null)
            {
                return(false);
            }

            return((user.PegsGiven != null && user.PegsGiven.Any()) || (user.PegsReceived != null && user.PegsReceived.Any()));
        }
Exemple #8
0
 private string CreateRemoveUserHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.RemoveUser }))
     {
         return("### How to remove users 🛑!\n" +
                $"1. To remove a user, type `@{_pockyBotSettings.BotName} {Commands.RemoveUser} {{@User}}|'{{username}}'`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #9
0
 private string CreateRoleConfigHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Config }))
     {
         return("### How to configure role config values 🗞️!\n" +
                $"1. To get/set/delete user roles, type `@{_pockyBotSettings.BotName} {Commands.RoleConfig} {Actions.Get}|{Actions.Set}|{Actions.Delete} {{@User}} {{role}}`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #10
0
 private string CreateNumberConfigHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Config }))
     {
         return("### How to configure number config values 🔢!\n" +
                $"1. To get/edit/refresh/delete number config values, type `@{_pockyBotSettings.BotName} {Commands.NumberConfig} {Actions.Get}|{Actions.Add}|{Actions.Delete} {{name}} {{number}}`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #11
0
 private string CreateFinishHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Finish }))
     {
         return("### How to complete the cycle 🚲!\n" +
                $"1. To display winners and results and clear the database, type `@{_pockyBotSettings.BotName} {Commands.Finish}`.\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #12
0
 private string CreateResetHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Reset }))
     {
         return("### How to reset all pegs 🙅!\n" +
                $"1. To clear all pegs, type `@{_pockyBotSettings.BotName} {Commands.Reset}`.\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #13
0
 private string CreateResultsHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Results }))
     {
         return("### How to display the results 📃!\n" +
                $"1. To display results, type `@{_pockyBotSettings.BotName} {Commands.Results}`.\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #14
0
 internal void TestShouldTriggerInDirectMessage(PockyUser user, Message message, ITrigger trigger,
                                                bool shouldTrigger)
 {
     this.Given(x => GivenAPockyUser(user))
     .And(x => GivenAMessage(message))
     .And(x => GivenATrigger(trigger))
     .When(x => WhenCallingShouldTriggerInDirectMessage())
     .Then(x => ThenItShouldReturn(shouldTrigger))
     .BDDfy();
 }
Exemple #15
0
 private string CreateLocationWeightHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Config }))
     {
         return("### How to configure location weight values ⚖️!\n" +
                $"1. To get/edit/delete location weight values, type `@{_pockyBotSettings.BotName} {Commands.LocationWeight} {Actions.Get}|{Actions.Set}|{Actions.Delete} {{location1}} {{location2}} {{weight}}`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #16
0
 internal void TestSuccessfulDeleteRoleConfig(Message message, Person person, PockyUser pockyUser,
                                              Message response, string userId, Role role)
 {
     this.Given(x => GivenAMessage(message))
     .And(x => GivenAPerson(person))
     .And(x => GivenAPockyUser(pockyUser))
     .When(x => WhenRespondingToAMessage())
     .Then(x => ThenItShouldReturnAResponse(response))
     .And(x => ThenItShouldCallRemoveRole(userId, role))
     .BDDfy();
 }
Exemple #17
0
 internal void TestUnsuccessfulSetRoleConfig(Message message, Person person, PockyUser pockyUser,
                                             Message response)
 {
     this.Given(x => GivenAMessage(message))
     .And(x => GivenAPerson(person))
     .And(x => GivenAPockyUser(pockyUser))
     .When(x => WhenRespondingToAMessage())
     .Then(x => ThenItShouldReturnAResponse(response))
     .And(x => ThenItShouldNotCallAddRole())
     .BDDfy();
 }
Exemple #18
0
 internal void TestShouldTriggerInRoom(PockyBotSettings settings, PockyUser user, Message message,
                                       ITrigger trigger, bool shouldTrigger)
 {
     this.Given(x => GivenPockyBotSettings(settings))
     .And(x => GivenAPockyUser(user))
     .And(x => GivenAMessage(message))
     .And(x => GivenATrigger(trigger))
     .When(x => WhenCallingShouldTriggerInRoom())
     .Then(x => ThenItShouldReturn(shouldTrigger))
     .BDDfy();
 }
Exemple #19
0
 private void GivenAPockyUser(PockyUser user)
 {
     if (user != null)
     {
         _pockyUserRepository.GetUser(user.UserId).Returns(user);
     }
     else
     {
         _pockyUserRepository.GetUser(Arg.Any <string>()).ReturnsForAnyArgs((PockyUser)null);
     }
 }
Exemple #20
0
 private string CreateStringConfigHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Config }))
     {
         return("### How to configure string config values 🎻!\n" +
                $"1. To get/add/delete string config values, type `@{_pockyBotSettings.BotName} {Commands.StringConfig} {string.Join("|",ConfigActions.All())} {{name}} {{value}}`\n" +
                $"    * Example 1: To add a keyword called \"amazing\", type `@{_pockyBotSettings.BotName} {Commands.StringConfig} {ConfigActions.Add} keyword amazing`\n" +
                $"    * Example 2: To add a linked keyword called \"awesome\" to the \"amazing\" keyword, type `@{_pockyBotSettings.BotName} {Commands.StringConfig} {ConfigActions.Add} linkedKeyword amazing:awesome`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return(CreateDefaultHelpMessage());
 }
Exemple #21
0
        public async Task GivePeg(string comment, PockyUser sender, PockyUser receiver, int numPegsGiven)
        {
            var peg = new Peg
            {
                SenderId   = sender.UserId,
                ReceiverId = receiver.UserId,
                Comment    = comment
            };
            await _pegRepository.CreatePeg(peg).ConfigureAwait(false);

            await PmSender(sender, receiver, numPegsGiven).ConfigureAwait(false);
            await PmReceiver(comment, sender, receiver).ConfigureAwait(false);
        }
Exemple #22
0
 internal void TestGivePeg(string comment, PockyUser sender, PockyUser receiver, int numPegsGiven,
                           Peg peg, Message senderPm, Message receiverPm)
 {
     this.Given(x => GivenAComment(comment))
     .And(x => GivenASender(sender))
     .And(x => GivenAReceiver(receiver))
     .And(x => GivenNumPegsGiven(numPegsGiven))
     .When(x => WhenGivingAPeg())
     .Then(x => ThenItShouldCallCreatePeg(peg))
     .And(x => ThenItShouldPmTheSender(senderPm))
     .And(x => ThenItShouldPmTheReceiver(receiverPm))
     .BDDfy();
 }
Exemple #23
0
 private string CreateLocationConfigHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Config }))
     {
         return("### How to configure location config values 🌏!\n" +
                $"1. To get/edit/delete locations, type `@{_pockyBotSettings.BotName} {Commands.LocationConfig} {Actions.Get}|{Actions.Add}|{Actions.Delete} {{location}}`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return("### How to get location values 🌏!\n" +
            $"1. To get a list of locations, type `@{_pockyBotSettings.BotName} {Commands.LocationConfig} {Actions.Get}`\n" +
            "    * To configure locations, please ask an admin.\n" +
            "1. I will respond in the room you messaged me in.");
 }
Exemple #24
0
 internal void TestRespond(Message message, PockyUser sender, int limit, List <Tuple <string, bool> > pegValidity, Message expectedResponse)
 {
     this.Given(x => GivenAMessage(message))
     .And(x => GivenAPockyUser(sender))
     .And(x => GivenAStringConfig("keyword", new List <string> {
         "keyword1", "keyword2"
     }))
     .And(x => GivenAStringConfig("penaltyKeyword", new List <string> {
         "penaltyKeyword"
     }))
     .And(x => GivenAGeneralConfig("requireValues", 1))
     .And(x => GivenAGeneralConfig("limit", limit))
     .And(x => GivenPegValidity(pegValidity))
     .When(x => WhenRespondingToAMessage())
     .Then(x => ThenItShouldReturnAResponse(expectedResponse))
     .BDDfy();
 }
Exemple #25
0
 private string CreateUserLocationHelpMessage(PockyUser user)
 {
     if (HasPermission(user, new[] { Role.Admin, Role.Config }))
     {
         return("### How to configure user location values!\n" +
                $"1. To get user locations for yourself or others, type `@{_pockyBotSettings.BotName} {Commands.UserLocation} {Actions.Get} me|all|unset|@User`\n" +
                $"1. To set user locations, type `@{_pockyBotSettings.BotName} {Commands.UserLocation} {Actions.Set} {{location}} me|@User1 @User2`\n" +
                $"1. To delete user locations, type `@{_pockyBotSettings.BotName} {Commands.UserLocation} {Actions.Delete} me|@User1 @User2`\n" +
                "1. I will respond in the room you messaged me in.");
     }
     return("### How to config your user location value!\n" +
            $"1. To get user locations for yourself or others, type `@{_pockyBotSettings.BotName} {Commands.UserLocation} {Actions.Get} me|all|unset|@User`\n" +
            $"1. To set your user location, type `@{_pockyBotSettings.BotName} {Commands.UserLocation} {Actions.Set} {{location}} me`\n" +
            "    * To bulk configure user locations, please ask an admin.\n" +
            $"1. To delete your user location, type `@{_pockyBotSettings.BotName} {Commands.UserLocation} {Actions.Delete} me`\n" +
            "1. I will respond in the room you messaged me in.");
 }
Exemple #26
0
        private string CreateCommandListMessage(PockyUser user)
        {
            var newMessage = "## What I can do (List of Commands)\n\n" +
                             $"* {Commands.Peg}\n" +
                             $"* {Commands.Status}\n" +
                             $"* {Commands.Keywords}\n" +
                             $"* {Commands.Ping}\n" +
                             $"* {Commands.Welcome}\n" +
                             $"* {Commands.Rotation}\n" +
                             $"* {Commands.LocationConfig}\n" +
                             $"* {Commands.UserLocation}\n";

            if (HasPermission(user, new [] { Role.Admin, Role.Results }))
            {
                newMessage += $"* {Commands.Results}\n";
            }


            if (HasPermission(user, new [] { Role.Admin, Role.Reset }))
            {
                newMessage += $"* {Commands.Reset}\n";
            }

            if (HasPermission(user, new [] { Role.Admin, Role.Finish }))
            {
                newMessage += $"* {Commands.Finish}\n";
            }

            if (HasPermission(user, new [] { Role.Admin, Role.Config }))
            {
                newMessage += $"* {Commands.NumberConfig}\n";
                newMessage += $"* {Commands.StringConfig}\n";
                newMessage += $"* {Commands.RoleConfig}\n";
                newMessage += $"* {Commands.LocationWeight}\n";
            }

            if (HasPermission(user, new [] { Role.Admin, Role.RemoveUser }))
            {
                newMessage += $"* {Commands.RemoveUser}\n";
            }

            newMessage += $"\nFor more information on a command type `@{_pockyBotSettings.BotName} help command-name` or direct message me with `help command-name`\n";
            newMessage += "\nI am still being worked on, so more features to come.";
            return(newMessage);
        }
Exemple #27
0
        public async Task UpsertUserLocation(PockyUser user, string location)
        {
            var userLocation = await _context.UserLocations.FirstOrDefaultAsync(x =>
                                                                                x.UserId == user.UserId);

            if (userLocation == null)
            {
                userLocation = new UserLocation
                {
                    Location = location,
                    UserId   = user.UserId
                };
                await _context.UserLocations.AddAsync(userLocation);
            }
            else
            {
                userLocation.Location = location;
                _context.UserLocations.Update(userLocation);
            }

            await _context.SaveChangesAsync();
        }
Exemple #28
0
 internal void TestRespond(Message message, string errorMessage, PockyUser senderUser,
                           PockyUser receiverUser, int limit, string comment, bool isPegValid, Person receiverChatUser,
                           Message response, bool givePeg)
 {
     this.Given(x => GivenAMessage(message))
     .And(x => GivenMessageValidity(errorMessage))
     .And(x => GivenAUser(senderUser))
     .And(x => GivenAUser(receiverUser))
     .And(x => GivenAStringConfig("keyword", new List <string> {
         "keyword1", "keyword2"
     }))
     .And(x => GivenAStringConfig("penaltyKeyword", new List <string> {
         "penaltyKeyword"
     }))
     .And(x => GivenAGeneralConfig("requireValues", 1))
     .And(x => GivenAGeneralConfig("limit", limit))
     .And(x => GivenPegValidity(comment, isPegValid))
     .And(x => GivenAChatUser(receiverChatUser))
     .When(x => WhenRespondingToAMessage())
     .Then(x => ThenItShouldReturnAResponse(response))
     .And(x => ThenItShouldCallGivePeg(givePeg))
     .BDDfy();
 }
Exemple #29
0
 private void GivenAUser(PockyUser user)
 {
     _pockyUserRepository.GetUser(user.UserId).Returns(user);
 }
Exemple #30
0
 private void GivenAReceiver(PockyUser receiver)
 {
     _receiver = receiver;
 }