private async Task Client_MessageUpdated(Cacheable <IMessage, ulong> arg1, SocketMessage arg2, ISocketMessageChannel arg3) { if (arg2.Author.IsBot || arg2.Author.IsWebhook) { return; } using var _db_ = DB(); var origMsg = await _db_.Messages.AsQueryable().FirstOrDefaultAsync(x => x.MessageId == cast(arg1.Id)); if (origMsg == null) { return; // TODO: add the message in } var currentContent = _db_.Contents.First(x => x.Id == origMsg.ContentId); if (currentContent.Content == arg2.Content) { return; } var newContent = new MsgContent() { Content = arg2.Content, Timestamp = arg2.EditedTimestamp.GetValueOrDefault(DateTimeOffset.Now).DateTime, Message = arg2.Id }; await _db_.Contents.AddAsync(newContent); await _db_.SaveChangesAsync(); origMsg.ContentId = newContent.Id; await _db_.SaveChangesAsync(); }
async Task AddMessage(IMessage arg, LogContext _db_ = null) { if (!(arg is IUserMessage umsg)) { return; } if (!(arg.Channel is IGuildChannel guildChannel)) { return; } if (arg.Author.IsBot || arg.Author.IsWebhook) { return; } var content = new MsgContent() { Message = umsg.Id, Content = umsg.Content, Timestamp = umsg.Timestamp.DateTime, }; bool dispose = false; if (_db_ == null) { dispose = true; _db_ = DB(); } _db_.Contents.Add(content); _db_.SaveChanges(); var msg = new MsgModel(umsg); msg.ContentId = content.Id; _db_.Messages.Add(msg); await _db_.SaveChangesAsync(); Console.ForegroundColor = ConsoleColor.Black; Console.BackgroundColor = ConsoleColor.White; Console.WriteLine($"{getWhere(umsg)}: {arg.Author.Username}: {arg.Content}"); Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; Info($"Starting attachment handle for {arg.Id} on thread {Thread.CurrentThread.Name} | {Thread.CurrentThread.ManagedThreadId}"); foreach (var attch in umsg.Attachments) { HandleAttachment(attch, guildChannel.Guild, umsg.Id); } Info($"Ended attachment handle for {arg.Id} on thread {Thread.CurrentThread.Name} | {Thread.CurrentThread.ManagedThreadId}"); if (dispose) { _db_.Dispose(); } }
public async Task <List <ReturnedMsg> > GetCombinedMsgs(ulong guild, ulong channel, ulong before = ulong.MaxValue, int limit = 25) { using var _db_ = DB(); var fromDb = await GetMessagesAsync(guild, channel, before, limit); var total = new List <ReturnedMsg>(); foreach (var x in fromDb) { total.Add(x); } var dsGuild = Program.Client.GetGuild(guild); if (dsGuild == null) { return(total); } var dsChnl = dsGuild.GetTextChannel(channel); if (dsChnl == null) { return(total); } IEnumerable <IMessage> dsMessages; if (before == ulong.MaxValue) { dsMessages = await dsChnl.GetMessagesAsync(limit).FlattenAsync(); } else { dsMessages = await dsChnl.GetMessagesAsync(before, Direction.Before, limit).FlattenAsync(); } bool changes = false; foreach (var x in total) { var inDs = dsMessages.FirstOrDefault(ds => ds.Id == x.Id); if (inDs == null) { x.IsDeleted = true; if (x.Content == null) { x.Content = "[Message content is not stored and cannot be fetched]"; } } else { x.Embeds = inDs.Embeds.ToList(); if (x.Content == null) { x.Content = inDs.Content; #if !DEBUG var msgContent = new MsgContent() { Message = inDs.Id, Timestamp = (inDs.EditedTimestamp ?? inDs.Timestamp).DateTime, Content = inDs.Content }; _db_.Contents.Add(msgContent); changes = true; #endif } } } foreach (var ds in dsMessages) { if (!(ds is IUserMessage umsg)) { continue; } if (!_db_.Messages.Any(x => x.MessageId == cast(ds.Id))) { total.Add(new DiscordMsg(this, umsg)); #if !DEBUG await Task.Run(async() => await AddMessage(umsg)); #endif } } if (changes) { await _db_.SaveChangesAsync(); } return(total); }