Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
scottAtTai committed Mar 19, 2023
1 parent 68b7499 commit 25a155d
Showing 1 changed file with 74 additions and 48 deletions.
122 changes: 74 additions & 48 deletions EQToolApis/Services/Discrod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IDiscordService
{
public void Login();
public List<Message> ReadMessages(long? lastid);
public List<Message> ReadMessageHistory(long? lastid);
}

public class DiscordServiceOptions
Expand Down Expand Up @@ -130,6 +131,26 @@ public List<Message> ReadMessages(long? lastid)
}
}

public List<Message> 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<Message>()
: Newtonsoft.Json.JsonConvert.DeserializeObject<List<Message>>(resultstring);
}
}

public class TimedHostedService : IHostedService, IDisposable
{
private readonly ILogger<TimedHostedService> _logger;
Expand All @@ -152,6 +173,56 @@ public Task StartAsync(CancellationToken stoppingToken)
return Task.CompletedTask;
}

private void AddMessages(List<Message> 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<EQTunnelAuctionItem>()
};
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)
Expand All @@ -167,54 +238,9 @@ private void DoWork(object? state)
{
var dbcontext = scope.ServiceProvider.GetRequiredService<EQToolContext>();
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<EQTunnelAuctionItem>()
};
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.");
Expand Down

0 comments on commit 25a155d

Please sign in to comment.