//Generate a message wrapper for an arbitrary non-discord message
 public MessageWrapper(string content)
 {
     this.content  = content;
     this.headless = true;
     this.Channel  = new ChannelWrapper();
     this.Author   = Program.ConsoleAuthor;
 }
Exemple #2
0
        private Task ReadyAsync()
        {
            if (!loadedConfig)
            {
                ConsoleAuthor = new UserWrapper(_client.CurrentUser.Username, _client.CurrentUser.Discriminator, _client.CurrentUser.Id, _client.CurrentUser.GetAvatarUrl(ImageFormat.Auto, 2048));
                botSettings.defaultUsername          = ConsoleAuthor.Username;
                botSettings.defaultUserDiscriminator = ConsoleAuthor.Discriminator;
                botSettings.defaultUserID            = ConsoleAuthor.Id;
                botSettings.defaultUserAvatar        = ConsoleAuthor.AvatarURL;
                var newSettings = JsonConvert.SerializeObject(botSettings);
                File.WriteAllText("config.json", newSettings);
            }
            else
            {
                ConsoleAuthor = new UserWrapper(botSettings.defaultUsername, botSettings.defaultUserDiscriminator, botSettings.defaultUserID, botSettings.defaultUserAvatar);
            }
            Console.WriteLine($"{_client.CurrentUser} is connected!");

            return(Task.CompletedTask);
        }
Exemple #3
0
        protected override void OnMessage(MessageEventArgs e)
        {
            //Console.WriteLine("Packet: "+e.Data);
            var pack = JsonConvert.DeserializeObject <WebPacket>(e.Data);

            switch (pack.packet)
            {
            case 0:
                if (!RCONUsersByToken.ContainsKey(pack.token))
                {
                    return;
                }
                var cs = JsonConvert.DeserializeObject <ConsolePacket>(e.Data);
                Console.WriteLine("From RCON Connection: " + cs.text);
                var console  = cs.text;
                var msg      = new MessageWrapper(console);
                var rconuser = RCONUsersByToken[pack.token];
                var us       = new UserWrapper(rconuser.Username, rconuser.Discriminator, rconuser.Id);
                msg.Channel = new RCONChannel()
                {
                    instance = Context.WebSocket
                };
                msg.Author = us;
                if (console.Length >= Program.botSettings.defaultPrefix.Length)
                {
                    var args = console.Split(' ');
                    var cmd  = args[0];
                    if (cmd.Substring(0, Program.botSettings.defaultPrefix.Length) == Program.botSettings.defaultPrefix)
                    {
                        cmd = cmd.Substring(Program.botSettings.defaultPrefix.Length);
                        if (Program.commands.ContainsKey(cmd))
                        {
                            try
                            {
                                Program.commands[cmd].Run(Program.commands[cmd].splitArgs(console), msg);
                                var returnString = "";
                                var returnInfo   = Program.commands[cmd].GetInputField(Program.commands[cmd].splitArgs(console), msg);
                                if (returnInfo.keepCommand)
                                {
                                    returnString = args[0];
                                }
                                if (returnInfo.keepArguments.Length > 0)
                                {
                                    returnString += " ";
                                }
                                foreach (var element in returnInfo.keepArguments)
                                {
                                    returnString += element + " ";
                                }
                                returnString += returnInfo.suffix;
                                var inputPacket = new ConsolePacket();
                                inputPacket.packet = 3;
                                inputPacket.text   = returnString;
                                Send(JsonConvert.SerializeObject(inputPacket));
                            }
                            catch (Exception se)
                            {
                                var errorPacket = new ConsolePacket();
                                errorPacket.packet = 0;
                                errorPacket.text   = "Failed to run command. Exception:" + Environment.NewLine + se.ToString();
                                Send(JsonConvert.SerializeObject(errorPacket));
                            }
                        }
                        else
                        {
                            Program.onUnknownCommand?.Invoke(msg);
                        }
                    }
                    else
                    {
                        var inputPacket = new ConsolePacket();
                        inputPacket.packet = 3;
                        inputPacket.text   = "";
                        Send(JsonConvert.SerializeObject(inputPacket));
                        foreach (var element in Program.rconConnections)
                        {
                            if (element.Key != Context.WebSocket)
                            {
                                var messagePack = new ConsolePacket();
                                messagePack.packet = 0;
                                messagePack.token  = pack.token;
                                messagePack.text   = "[RCON] " + rconuser.Username + ": " + console;
                                element.Key.Send(JsonConvert.SerializeObject(messagePack));
                            }
                        }
                    }
                }
                break;

            case 2:
                var returnPacket = new TokenReturnPacket();
                returnPacket.packet = 1;
                if (RCONUsersByToken.ContainsKey(pack.token))
                {
                    returnPacket.status   = true;
                    returnPacket.username = RCONUsersByToken[pack.token].Username;
                    Program.rconConnections[Context.WebSocket].logged = true;
                    Program.rconConnections[Context.WebSocket].token  = pack.token;
                    Console.WriteLine("RCON connection from " + Context.UserEndPoint.Address.ToString() + " logged in as " + returnPacket.username);
                }
                else
                {
                    returnPacket.status = false;
                    Program.rconConnections[Context.WebSocket].logged = false;
                    Program.rconConnections[Context.WebSocket].token  = "";
                    Console.WriteLine("RCON connection from " + Context.UserEndPoint.Address.ToString() + " failed to log in, used incorrect token '" + pack.token + "'");
                }
                Send(JsonConvert.SerializeObject(returnPacket));
                break;
            }
        }
 //Generate a message wrapper for an actual discord message
 public MessageWrapper(SocketMessage message)
 {
     this.message = message;
     this.Channel = new ChannelWrapper(message.Channel);
     this.Author  = new UserWrapper(message.Author);
 }