Пример #1
0
 public static Entity_ArkMessage ParseSingleMessage(string raw)
 {
     try
     {
         string            header  = raw.Substring(0, raw.Split(':')[0].Length);
         string            content = raw.Substring(raw.Split(':')[0].Length + 2);
         Entity_ArkMessage message = new Entity_ArkMessage();
         message.content = content;
         string[] para = header.Split('(');
         string   user = header.Substring(header.Length - 1 - para[para.Length - 1].Length).Trim('(').Trim(')');
         string   sn   = header.Substring(0, header.Length - 1 - para[para.Length - 1].Length);
         message.username  = user;
         message.steamName = sn;
         return(message);
     } catch
     {
         return(null); //Parse error.
     }
 }
Пример #2
0
        public async Task <int> GetAndSendNewChat()
        {
            //Return number of messages.
            string rawChats = await RunRCONCommand("getchat");

            //Check if we got any
            if (rawChats.ToLower().Contains("server received, but no response!!"))
            {
                //No chats. Return zero.
                return(0);
            }
            //Parse
            Entity_ArkMessage[] messages = Entity_ArkMessage.ParseMultipleMessages(rawChats);
            //Check if we're verifying an ArkLink
            foreach (Entity_ArkMessage message in messages)
            {
                //Check if it matches
                foreach (var e in Program.pendingArkLinks)
                {
                    if (message.content.Contains(e.randId))
                    {
                        //Matches!
                        await DiscordUser.FinishArkLink(message.username, message.steamName, e.msg, e.user.id);
                    }
                }
            }
            //Fetch Discord server
            //Todo: Catch this and alert the owner
            var server = await Program.discord.GetChannelAsync(notificationChannel);

            //Loop through
            foreach (Entity_ArkMessage message in messages)
            {
                //Create an embed
                var embed = Tools.GenerateEmbed("Ark Message from " + message.username, "Steam name: " + message.steamName + "- Ark server: " + name, message.content, DSharpPlus.Entities.DiscordColor.LightGray);
                //Send this embed to the desired channel
                await server.SendMessageAsync(embed : embed);
            }
            return(messages.Length);
        }
Пример #3
0
        //Functions
        //Util

        /*
         * public async Task Cmd_StartServer(DSharpPlus.EventArgs.MessageCreateEventArgs e)
         * {
         *  var msg = await e.Message.RespondAsync(embed: Tools.GenerateEmbed("Server Starting...", "", "The server \"" + name + "\" is starting...", DSharpPlus.Entities.DiscordColor.Yellow));
         *  new Thread(() =>
         *  {
         *      Thread.CurrentThread.IsBackground = true;
         *
         *
         *      //Begin startup
         *      //StartServer();
         *      //Keep attempting to connect.
         *      DateTime start = DateTime.UtcNow;
         *      if (isRconEnabled)
         *      {
         *          //Rcon will connect.
         *          while (true)
         *          {
         *              //Try to connect.
         *              var rcon = ArkRconConnection.ConnectToRcon(rconIp, ushort.Parse(rconPort.ToString()), rconPassword).ConfigureAwait(false).GetAwaiter().GetResult();
         *              rconConnection = rcon;
         *              if (rcon.isReady)
         *              {
         *                  //Connected
         *                  var embed = Tools.GenerateEmbed("Server Started!", "", "The server is ready to use and join!", DSharpPlus.Entities.DiscordColor.Green);
         *                  processStatus = ArkServerProcessStatus.Online;
         *                  msg.ModifyAsync(embed: embed).ConfigureAwait(false).GetAwaiter().GetResult();
         *                  break;
         *              }
         *              TimeSpan time = DateTime.UtcNow - start;
         *              if (time.TotalSeconds > 120)
         *              {
         *                  //Timeout.
         *                  processStatus = ArkServerProcessStatus.Unknown;
         *                  var embed = Tools.GenerateEmbed("Server Timeout", "Timeout time is 120s.", "The connection timed out. The server could be up, but it is unknown.", DSharpPlus.Entities.DiscordColor.Red);
         *                  msg.ModifyAsync(embed: embed).ConfigureAwait(false).GetAwaiter().GetResult();
         *                  break;
         *              }
         *              Thread.Sleep(5000);
         *          }
         *      }
         *      else
         *      {
         *
         *      }
         *  }).Start();
         * }*/

        public async Task <Entity_ArkMessage[]> FetchMessages(string rawBuffer = "na")
        {
            //If RCON is off or not connected, return a blank array.
            if (isRconEnabled == false)
            {
                return(new Entity_ArkMessage[0]);
            }
            //Run RCON command if rawbuffer wasn't passed.
            if (rawBuffer == "na")
            {
                //Fetch
                rawBuffer = await RunRCONCommand("GetChat");
            }
            if (rawBuffer == "error")
            {
                //Error. Stop
                return(new Entity_ArkMessage[0]);
            }
            //Parse
            Entity_ArkMessage[] messageBuffer = Entity_ArkMessage.ParseMultipleMessages(rawBuffer);
            return(messageBuffer);
        }
Пример #4
0
        public static Entity_ArkMessage[] ParseMultipleMessages(string raw)
        {
            string[] lines = Tools.SplitRconByLine(raw);



            List <Entity_ArkMessage> messages = new List <Entity_ArkMessage>();

            //Go through each line and parse it.
            foreach (string line in lines)
            {
                if (line.Length < 3)
                {
                    continue;
                }
                Entity_ArkMessage msg = ParseSingleMessage(line);
                if (msg != null)
                {
                    messages.Add(msg);
                }
            }
            return(messages.ToArray());
        }