Ejemplo n.º 1
0
        public static async Task DeletedMessageEvent(Cacheable <IMessage, ulong> msg, ISocketMessageChannel channel)
        {
            try
            {
                var logChannel = DiscordContextOverseer.GetDeletedMessageLog();
                var message    = await msg.GetOrDownloadAsync();

                if (message != null)
                {
                    var embed = new EmbedBuilder();
                    List <CachedFile> filesData = null;
                    bool hasImages = false;

                    //if at least one image (embedded or attached) was in the deleted message, look for it in the cache
                    bool imageExists = CheckForImageWithinEmbeds(message);
                    bool hasURLs     = CheckForURLs(message);
                    if ((message.Embeds.Count > 0 && imageExists) || (message.Attachments.Count > 0 && hasURLs))
                    {
                        hasImages = true;
                        filesData = ImageCacheManager.FindImagesInCache(message.Id);
                    }
                    if (hasImages)
                    {
                        if (filesData != null)
                        {
                            embed.WithTitle($"🗑 {message.Author.Username}#{message.Author.Discriminator} deleted message containing {filesData.Count} image{((filesData.Count > 1) ? "s" : "") } in {message.Channel.Name}. UserID = {message.Author.Id}");
                        }
                        else
                        {
                            embed.WithTitle($"🗑 {message.Author.Username}#{message.Author.Discriminator} deleted message containing images that weren't cached in {message.Channel.Name}. UserID = {message.Author.Id}");
                        }
                    }
                    else //it's just a message
                    {
                        embed.WithTitle($"🗑 {message.Author.Username}#{message.Author.Discriminator} deleted message messageID {message.Id} in {message.Channel.Name}. UserID = {message.Author.Id}");
                    }

                    //check for other types of embeds
                    string extraMessageContent = SearchForOtherEmbedTypes(message);

                    embed.WithDescription(message.Content + extraMessageContent);
                    embed.WithColor(new Color(255, 0, 0));

                    await logChannel.SendMessageAsync("", false, embed.Build());

                    if (filesData != null)
                    {
                        foreach (var file in filesData)
                        {
                            using (var ms = new MemoryStream(file.File))
                            {
                                await logChannel.SendFileAsync(ms, file.FileName);
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                await ExceptionManager.LogExceptionAsync(ErrMessages.DeletedMessageException, ex);
            }
        }
Ejemplo n.º 2
0
        public static async Task EditedMessageEvent(Cacheable <IMessage, ulong> msgBefore, SocketMessage msgAfter, ISocketMessageChannel channel)
        {
            try
            {
                if (string.IsNullOrEmpty(msgAfter.Content))
                {
                    return;
                }
                if (msgBefore.Value.Content == msgAfter.Content)
                {
                    return;
                }

                var logChannel = DiscordContextOverseer.GetDeletedMessageLog();
                var message    = await msgBefore.GetOrDownloadAsync();

                if (message != null)
                {
                    List <byte[]> imagesData = null;
                    bool          hasImages  = false;

                    string time = msgAfter.Timestamp.DateTime.ToLongDateString() + " " + msgAfter.Timestamp.DateTime.ToLongTimeString();

                    var embed = new EmbedBuilder();
                    if (hasImages)
                    {
                        if (imagesData != null)
                        {
                            embed.WithTitle($"✍️ {msgAfter.Author.Username}#{msgAfter.Author.Discriminator} edited messageID {message.Id} at {time} containing {imagesData.Count} image{((imagesData.Count > 1) ? "" : "s") }");
                        }
                        else
                        {
                            embed.WithTitle($"✍️ {msgAfter.Author.Username}#{msgAfter.Author.Discriminator} edited messageID {message.Id} at {time} containing images that weren't cached");
                        }
                    }
                    else
                    {
                        embed.WithTitle($"🗑 {message.Author.Username}#{message.Author.Discriminator} deleted message in {message.Channel.Name}. UserID = {message.Author.Id}");
                    }
                    embed.WithTitle($"✍️ {msgAfter.Author.Username}#{msgAfter.Author.Discriminator} edited messageID {message.Id} at {time}");
                    embed.WithDescription($"in #{channel.Name}, Original: " + msgBefore.Value.Content);
                    embed.WithColor(new Color(250, 255, 0));

                    await logChannel.SendMessageAsync("", false, embed.Build());

                    if (imagesData != null)
                    {
                        foreach (byte[] image in imagesData)
                        {
                            using (var ms = new MemoryStream(image))
                            {
                                await logChannel.SendFileAsync(ms, "cached.png");
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                ExceptionManager.HandleException($"{typeof(MessageLogger).GetType().FullName}: {ExceptionManager.GetAsyncMethodName()}", ex);
            }
        }