diff --git a/EQToolApis/Services/Discrod.cs b/EQToolApis/Services/Discrod.cs index 1be56d9b..6b766e28 100644 --- a/EQToolApis/Services/Discrod.cs +++ b/EQToolApis/Services/Discrod.cs @@ -9,6 +9,7 @@ public interface IDiscordService { public void Login(); public List ReadMessages(long? lastid); + public List ReadMessageHistory(long? lastid); } public class DiscordServiceOptions @@ -130,6 +131,26 @@ public List ReadMessages(long? lastid) } } + public List ReadMessageHistory(long? lastid) + { + var url = "https://discord.com/api/v9/channels/672512233435168784/messages?limit=50"; + if (lastid.HasValue) + { + url = $"https://discord.com/api/v9/channels/672512233435168784/messages?before={lastid.Value}&limit=50"; + } + using (var msg = new HttpRequestMessage()) + { + msg.Headers.Add("authorization", token); + msg.RequestUri = new Uri(url); + msg.Method = HttpMethod.Get; + var result = _client.SendAsync(msg).Result; + var resultstring = result.Content.ReadAsStringAsync().Result; + return result.StatusCode == System.Net.HttpStatusCode.Unauthorized + ? new List() + : Newtonsoft.Json.JsonConvert.DeserializeObject>(resultstring); + } + } + public class TimedHostedService : IHostedService, IDisposable { private readonly ILogger _logger; @@ -152,6 +173,56 @@ public Task StartAsync(CancellationToken stoppingToken) return Task.CompletedTask; } + private void AddMessages(List messages, EQToolContext dbcontext) + { + foreach (var item in messages) + { + var embed = item.embeds.FirstOrDefault(); + if (embed != null && embed.fields != null) + { + var eqplayer = dbcontext.EQAuctionPlayers.FirstOrDefault(a => a.Name == embed.AuctionPerson); + if (eqplayer == null) + { + eqplayer = new EQAuctionPlayer + { + Name = embed.AuctionPerson.Trim() + }; + _ = dbcontext.EQAuctionPlayers.Add(eqplayer); + _ = dbcontext.SaveChanges(); + } + + var m = new DB.Models.EQTunnelMessage + { + AuctionType = embed.AuctionType, + EQAuctionPlayerId = eqplayer.EQAuctionPlayerId, + DiscordMessageId = item.id, + Server = Servers.Green, + TunnelTimestamp = embed.timestamp, + EQTunnelAuctionItems = new List() + }; + foreach (var it in embed.fields) + { + var eqitem = dbcontext.EQitems.FirstOrDefault(a => a.ItemName == it.ItemName); + if (eqitem == null) + { + eqitem = new EQitem + { + ItemName = it.ItemName + }; + _ = dbcontext.EQitems.Add(eqitem); + _ = dbcontext.SaveChanges(); + } + m.EQTunnelAuctionItems.Add(new EQTunnelAuctionItem + { + AuctionPrice = it.Price, + EQitemId = eqitem.EQitemId + }); + } + _ = dbcontext.EQTunnelMessages.Add(m); + } + } + _ = dbcontext.SaveChanges(); + } private void DoWork(object? state) { if (Processing) @@ -167,54 +238,9 @@ private void DoWork(object? state) { var dbcontext = scope.ServiceProvider.GetRequiredService(); var lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderByDescending(a => a).FirstOrDefault(); - var messages = discordService.ReadMessages(lastidread); - foreach (var item in messages) - { - var embed = item.embeds.FirstOrDefault(); - if (embed != null && embed.fields != null) - { - var eqplayer = dbcontext.EQAuctionPlayers.FirstOrDefault(a => a.Name == embed.AuctionPerson); - if (eqplayer == null) - { - eqplayer = new EQAuctionPlayer - { - Name = embed.AuctionPerson.Trim() - }; - _ = dbcontext.EQAuctionPlayers.Add(eqplayer); - _ = dbcontext.SaveChanges(); - } - - var m = new DB.Models.EQTunnelMessage - { - AuctionType = embed.AuctionType, - EQAuctionPlayerId = eqplayer.EQAuctionPlayerId, - DiscordMessageId = item.id, - Server = Servers.Green, - TunnelTimestamp = embed.timestamp, - EQTunnelAuctionItems = new List() - }; - foreach (var it in embed.fields) - { - var eqitem = dbcontext.EQitems.FirstOrDefault(a => a.ItemName == it.ItemName); - if (eqitem == null) - { - eqitem = new EQitem - { - ItemName = it.ItemName - }; - _ = dbcontext.EQitems.Add(eqitem); - _ = dbcontext.SaveChanges(); - } - m.EQTunnelAuctionItems.Add(new EQTunnelAuctionItem - { - AuctionPrice = it.Price, - EQitemId = eqitem.EQitemId - }); - } - _ = dbcontext.EQTunnelMessages.Add(m); - } - } - _ = dbcontext.SaveChanges(); + AddMessages(discordService.ReadMessages(lastidread), dbcontext); + lastidread = dbcontext.EQTunnelMessages.Where(a => a.Server == Servers.Green).Select(a => (long?)a.DiscordMessageId).OrderBy(a => a).FirstOrDefault(); + AddMessages(discordService.ReadMessageHistory(lastidread), dbcontext); } _logger.LogInformation("Timed Hosted Service is working.");