示例#1
0
        private void ReactionTracker()
        {
            try
            {
                Client.ReactionAdded += async(cachedMsg, msg, reaction) =>
                {
                    if (reaction.User.Value.IsBot)
                    {
                        return;
                    }

                    IMessage reactedMessage = await reaction.Channel.GetMessageAsync(cachedMsg.Id);

                    if (reaction.User.Value.Id == reactedMessage.Author.Id)
                    {
                        return;
                    }

                    DiscordReaction reactionObj;

                    if ((reaction.Emote as Emoji) == null)
                    {
                        reactionObj = new DiscordReaction(reaction.MessageId.ToString(), reaction.Channel.Id.ToString(), reactedMessage.Author.Id.ToString(), reaction.UserId.ToString(), (reaction.Emote as Emote).Id.ToString(), reaction.Emote.Name, DateTime.Now);
                    }
                    else
                    {
                        reactionObj = new DiscordReaction(reaction.MessageId.ToString(), reaction.Channel.Id.ToString(), reactedMessage.Author.Id.ToString(), reaction.UserId.ToString(), (reaction.Emote as Emoji).Name, DateTime.Now);
                    }

                    using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                    {
                        DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                        await DBContext.AddAsync(reactionObj);

                        await DBContext.SaveChangesAsync();
                    }
                };

                Client.ReactionRemoved += async(cachedMsg, msg, reaction) =>
                {
                    if (reaction.User.Value.IsBot)
                    {
                        return;
                    }

                    IMessage reactedMessage = await reaction.Channel.GetMessageAsync(cachedMsg.Id);

                    if (reaction.User.Value.Id == reactedMessage.Author.Id)
                    {
                        return;
                    }

                    using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                    {
                        DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                        DiscordReaction existingReaction = DBContext.Reactions.FirstOrDefault(x => x.ReceiverId == reactedMessage.Author.Id.ToString() && x.ReactorId == reaction.UserId.ToString() && x.ReactionName == reaction.Emote.Name && x.Id == reaction.MessageId.ToString());
                        if (existingReaction != null)
                        {
                            DBContext.Remove(existingReaction);
                        }

                        await DBContext.SaveChangesAsync();
                    }
                };

                Client.ReactionsCleared += async(msgCache, msgChannel) =>
                {
                    using (BotDBContext DBContext = DBFactory.Create <BotDBContext>())
                    {
                        DBContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
                        IUserMessage fetchedMsg = await msgCache.GetOrDownloadAsync();

                        DBContext.Reactions.RemoveRange(DBContext.Reactions.FromSql("SELECT * FROM Reactions WHERE Id = {0}", fetchedMsg.Id).AsNoTracking().ToList());
                        ConsoleEx.WriteColoredLine($"Reactions cleared from message ID($[[DarkCyan]]${fetchedMsg.Id}$[[Gray]]$.");
                        await DBContext.SaveChangesAsync();
                    }
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }