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); }
//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); }