public async Task ReceivedMessage(TrackerMessage message) { if (message.IsAck) { if (WaitingForAck.ContainsKey(message.AckId)) { WaitingForAck.Remove(message.AckId); await Program.MopsLog(new Discord.LogMessage(Discord.LogSeverity.Info, "", $"Received awaited ACK {message.AckId}")); } } else { //ToDo: Handle request var args = message.Content.Split("|||"); var trackerType = Enum.Parse <MopsBot.Data.Tracker.BaseTracker.TrackerType>(args[1]); var result = ""; try { switch (args.First()) { case "ADD": await StaticBase.Trackers[trackerType].AddTrackerAsync(args[3], ulong.Parse(args[2]), args[4]); result = "Added the tracker successfully"; break; case "REMOVE": var worked = await StaticBase.Trackers[trackerType].TryRemoveTrackerAsync(args[2], ulong.Parse(args[3])); result = worked ? "Removed successfully" : "Could not find the tracker"; break; default: break; } } catch (Exception e) { result = e.InnerException.Message; } await SendMessage($"ACKNOWLEDGED REQUEST {message.Id}\n{result}", false); } }
public async Task SendMessage(string message, bool waitForAck = true) { int id = eventId++; var tMessage = new TrackerMessage(id, message); if (waitForAck) { WaitingForAck[id] = tMessage; } try { var stream = client.GetStream(); lock (sendLock) { stream.WriteAsync(Encoding.ASCII.GetBytes(tMessage.FullMessage)).AsTask().Wait(); } } catch (Exception e) { await Program.MopsLog(new Discord.LogMessage(Discord.LogSeverity.Error, "", $"Error on sending message", e)); } }