public async Task <IActionResult> PutUserStrikes(int id, UserStrikes userStrikes)
        {
            if (id != userStrikes.IduserStrikes)
            {
                return(BadRequest());
            }

            _context.Entry(userStrikes).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserStrikesExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult <UserStrikes> > PostUserStrikes(UserStrikes userStrikes)
        {
            _context.UserStrikes.Add(userStrikes);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetUserStrikes", new { id = userStrikes.IduserStrikes }, userStrikes));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Print the user's strikes
        /// </summary>
        /// <param name="e"></param>
        /// <param name="message"></param>
        private void PrintStrikes(ServerMessage e, string message)
        {
            if (!on)
            {
                return;
            }

            var channel = e.Author.GetOrCreateDMChannelAsync().Result as SocketDMChannel;

            // GetOrCreateDMChannelAsync returns null the first time always, for some reason.
            // This is why we're waiting a second, and then calling it a second time. Kind of weird.
            // If you can find a way to make it work the first time, that would be great.
            Task.Delay(1000).ContinueWith((a) =>
            {
                channel = e.Author.GetOrCreateDMChannelAsync().Result as SocketDMChannel;

                // Print the appropriate response
                if (!UserStrikes.ContainsKey(e.Author.Id) || UserStrikes[e.Author.Id].Count((s) => !s.Resolved) == 0)
                {
                    server.safeSendMessage(channel, "You do not have any strikes at the present moment.\nGood job! Please continue to be kind, unwavering, and awesome!");
                }
                else
                {
                    var strikes = "Here is a list of all your strikes:\n";
                    strikes    += PrintStrikesNicely(UserStrikes[e.Author.Id]);
                    server.safeSendMessage(channel, strikes);
                }
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Resolves all strikes a specific user has, with the same reason
        /// </summary>
        /// <param name="e"></param>
        /// <param name="m"></param>
        private void ResolveAllStrikes(ServerMessage e, string message)
        {
            if (!on)
            {
                return;
            }

            string[] split = message.Split(new char[] { ' ' }, 2);

            if (split.Length == 2 && Funcs.GetUserByMentionOrName(server.getServer().Users, split[0]) != null)
            {
                var user     = Funcs.GetUserByMentionOrName(server.getServer().Users, split[0]);
                var userName = user.Nickname ?? user.Username;

                // If the user doesn't have strikes to resolve, print a response
                if (!UserStrikes.ContainsKey(user.Id) || UserStrikes[user.Id].All((s) => s.Resolved))
                {
                    server.safeSendMessage(e.Channel, userName + " does not have any strikes to resolve.");
                }
                else
                {
                    foreach (var strike in UserStrikes[user.Id].Where((s) => !s.Resolved))
                    {
                        strike.Resolved      = true;
                        strike.ResolveReason = split[1];
                    }

                    UserStrikes.persist();

                    server.safeSendMessage(e.Channel, "All of " + userName + "'s unresolved strikes have been resolved.");
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gives a strike to a mentioned user with a specific reason
        /// </summary>
        /// <param name="e"></param>
        /// <param name="message"></param>
        private void StrikeUser(ServerMessage e, string message)
        {
            if (!on)
            {
                return;
            }

            string[] split = message.Split(new char[] { ' ' }, 2);

            if (split.Length == 2 && Funcs.GetUserByMentionOrName(server.getServer().Users, split[0]) != null)
            {
                var user     = Funcs.GetUserByMentionOrName(server.getServer().Users, split[0]);
                var userName = user.Nickname ?? user.Username;

                var strike = new StrikeDetails()
                {
                    StrikeDate = new DateTimeOffset(DateTime.Now).ToString(),
                    Reason     = split[1]
                };

                // If the user didn't have any previous strikes, create an entry for them
                if (!UserStrikes.ContainsKey(user.Id))
                {
                    var strikes = new List <StrikeDetails>()
                    {
                        strike
                    };

                    UserStrikes.Add(user.Id, strikes);
                    UserStrikes.persist();
                }
                // The user already has an entry, add the strike to the others
                else
                {
                    UserStrikes[user.Id].Add(strike);
                    UserStrikes.persist();

                    // Alert for 3 or more unresolved strikes
                    if (UserStrikes[user.Id].Count((s) => !s.Resolved) >= 4)
                    {
                        server.safeSendMessage(StrikeChannel, server.getServer().EveryoneRole + " " + userName + " has been given their third (or more) strike in " + (e.Channel as SocketTextChannel).Mention + ".\n Reason: " + strike.Reason);
                    }
                }
                if (UserStrikes[user.Id].Count((s) => !s.Resolved) >= 2)
                {
                    server.safeSendMessage(e.Channel, "The user " + userName + " has been given a strike for the following reason: " + split[1]);
                }
                else
                {
                    server.safeSendMessage(e.Channel, "The user " + userName + "has been given a warning for the following reason: " + split[1]);
                }
            }
            else
            {
                server.safeSendMessage(e.Channel, "Invalid format! Please use ``!strike <user mention> <reason>");
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Resolves a specific strike
        /// </summary>
        /// <param name="e"></param>
        /// <param name="message"></param>
        private void ResolveStrike(ServerMessage e, string message)
        {
            if (!on)
            {
                return;
            }

            string[] split = message.Split(new char[] { ' ' }, 3);

            if (split.Length == 3 && Funcs.GetUserByMentionOrName(server.getServer().Users, split[0]) != null)
            {
                var user     = Funcs.GetUserByMentionOrName(server.getServer().Users, split[0]);
                var userName = user.Nickname ?? user.Username;

                int strikeID;
                // Print an appropriate response to each scenario
                if (!UserStrikes.ContainsKey(user.Id))
                {
                    server.safeSendMessage(e.Channel, userName + " does not have any strikes to resolve.");
                }
                else if (!int.TryParse(split[1], out strikeID))
                {
                    server.safeSendMessage(e.Channel, "Could not parse the strike number to resolve.");
                }
                else if (strikeID < 1 || strikeID > UserStrikes[user.Id].Count)
                {
                    server.safeSendMessage(e.Channel, "There are no strikes that correspond to that number.");
                }
                else if (UserStrikes[user.Id][strikeID - 1].Resolved)
                {
                    server.safeSendMessage(e.Channel, "That specific strike has been resolved already.");
                }
                // Resolve the strike
                else
                {
                    UserStrikes[user.Id][strikeID - 1].Resolved      = true;
                    UserStrikes[user.Id][strikeID - 1].ResolveReason = split[2];
                    UserStrikes.persist();
                    server.safeSendMessage(e.Channel, userName + "'s strike has been resolved.");
                }
            }
        }
Ejemplo n.º 7
0
        private void cleanupStrikes()
        {
            if (daysTillAutoresolve == 0)
            {
                return;
            }
            var resolveTime   = DateTimeOffset.Now - TimeSpan.FromDays(daysTillAutoresolve);
            var resolveString = "Autoresolve after " + daysTillAutoresolve.ToString() + " days.";

            foreach (var userId in UserStrikes.Keys)
            {
                foreach (var strike in UserStrikes[userId].Where((s) => !s.Resolved && s._strikeDate < resolveTime))
                {
                    strike.Resolved      = true;
                    strike.ResolveReason = resolveString;
                }
            }
            UserStrikes.persist();
            Timer t       = new Timer((TimeSpan.FromDays(daysTillAutoresolve)).TotalMilliseconds);
            var   daysOld = daysTillAutoresolve;

            t.Elapsed += (a, b) =>
            {
                if (t != null)
                {
                    t.Stop();
                    t.Dispose();
                }
                if (daysOld == daysTillAutoresolve) //only restarts timer if nothing else has been messing with this
                {
                    cleanupStrikes();
                }
            };
            t.AutoReset = false;
            t.Start();
        }