Exemple #1
0
        private Task MessageReceived(SocketMessage msg)
        {
            if (msg.Content.StartsWith(_MSG_PREFIX))
            {
                lock (_lock) {
                    String content = msg.Content;
                    int    plen    = _MSG_PREFIX.Length;
                    content = content.Substring(plen, content.Length - plen);
                    _msgQueue.Enqueue(new DiscordMessage(msg.Author.Id, content));
                }
                string user    = msg.Author.Username;
                string discrim = msg.Author.Discriminator;
                Console.WriteLine($"--> SERVER: Queued Discord message (from {user}#{discrim})");
            }
            else if (msg.Content.StartsWith(_ADMIN_PREFIX))
            {
                string friendlyName = DiscordUtil.GetUniqueName(msg.Author);
                if (!_adminList.IsAdmin(msg.Author))
                {
                    Console.WriteLine($"--> SERVER: Requested admin command but was rejected ({friendlyName})");
                    return(Task.CompletedTask);
                }

                String content = msg.Content;
                int    plen    = _ADMIN_PREFIX.Length;
                content = content.Substring(plen, content.Length - plen);
                string[] tokens = content.Split(" ");
                if (tokens.Length > 1)
                {
                    if (tokens[0] == "list" && tokens[1] == "admins")
                    {
                        string adminStr = String.Join(", ", _adminList.GetAdmins());
                        Console.WriteLine($"--> SERVER: Requested admins ({friendlyName})");
                        Console.WriteLine($"--> SERVER: {adminStr}");
                        msg.Channel.SendMessageAsync($"Current users with admin permissions are: {adminStr}");
                    }
                    else if (tokens[0] == "add" && tokens[1] == "admin")
                    {
                        var user = msg.MentionedUsers.FirstOrDefault();
                        if (user == null)
                        {
                            Console.WriteLine("--> SERVER: Unknown user.");
                            return(Task.CompletedTask);
                        }
                        bool success = _adminList.AddAdmin(user);
                        Console.WriteLine($"--> SERVER: Giving admin to {friendlyName}");
                        Console.WriteLine($"--> SERVER: {_adminList.Message}");
                        if (success)
                        {
                            msg.Channel.SendMessageAsync($"I've added {friendlyName} as an admin.");
                        }
                        else
                        {
                            msg.Channel.SendMessageAsync($"I couldn't add that user as an admin: {_adminList.Message}");
                        }
                    }
                    else if (tokens[0] == "remove" && tokens[1] == "admin")
                    {
                        var user = msg.MentionedUsers.FirstOrDefault();
                        if (user == null)
                        {
                            Console.WriteLine("--> SERVER: Unknown user.");
                            return(Task.CompletedTask);
                        }
                        bool success = _adminList.RemoveAdmin(user);
                        Console.WriteLine($"--> SERVER: Revoking admin from {friendlyName}");
                        Console.WriteLine($"--> SERVER: {_adminList.Message}");
                        if (success)
                        {
                            msg.Channel.SendMessageAsync($"I've removed {friendlyName} as an admin.");
                        }
                        else
                        {
                            msg.Channel.SendMessageAsync($"I couldn't remove that user as an admin: {_adminList.Message}");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("--> SERVER: Unknown admin command received.");
                }
            }

            return(Task.CompletedTask);
        }