Exemple #1
0
        private async Task OnMessageReceivedAsync(SocketMessage s)
        {
            if (!(s is SocketUserMessage msg))
            {
                return;
            }

            string prefix = _config["prefix"];

            if (!IsCommandOrBot(msg, prefix, out int argPos))
            {
                User          usr       = Users.GetOrCreateUser(msg.Author);
                List <string> allswears = DataStorage.GetAllSwears();
                if ((allswears == null) || (allswears.Count() == 0))
                {
                    return;
                }
                string rxstring  = "(?:\\b|^)(?<swear>" + string.Join("|", allswears) + ")(?:\\s|$)";
                Regex  rx        = new Regex(rxstring, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                var    swearlist = rx.Matches(msg.Content)
                                   .OfType <Match>()
                                   .Select(m => m.Groups[1].Value)
                                   .OrderBy(n => n);

                var    swearlistdistinct = swearlist.Distinct();
                int    len  = swearlistdistinct.Count();
                string said = "";
                if (len < 1)
                {
                    return;
                }
                foreach (var swear in swearlist)
                {
                    Users.AddOrIncrementUsed(usr, swear);
                    Users.IncrementOwed(usr);
                }
                if (len == 1)
                {
                    said = swearlistdistinct.FirstOrDefault();
                }
                else if (len == 2)
                {
                    said = string.Join(" and ", swearlistdistinct);
                }
                else
                {
                    said = string.Join(", ", swearlistdistinct.Take(len - 1)) + ", and " + swearlistdistinct.LastOrDefault();
                }

                await msg.Channel.SendMessageAsync($"{msg.Author.Username}, how could you say {said}!");
            }
        }