public static async Task ReactionAdded(Cacheable <IUserMessage, ulong> cache, ISocketMessageChannel channel, SocketReaction reaction) { _ownerGiveawayCache = MemoryCache.OwnerGiveawaysCache; _previousReactions = MemoryCache.OwnerGiveawayReactions; _validCache = _ownerGiveawayCache.Where(x => !x.HasExpired); _validIds = _validCache.Select(x => x.MessageId).ToHashSet(); ulong msgId = _validIds.FirstOrDefault(x => x == reaction.MessageId); bool giveawayReaction = msgId != 0; if (!giveawayReaction) { return; } OwnerGiveaway validGiveaway = _ownerGiveawayCache.First(x => x.MessageId == msgId); if (_previousReactions == null) { return; } if (!reaction.User.IsSpecified || reaction.User.Value.IsBot || validGiveaway == null) { return; } if (_previousReactions.Any(x => x.UserId == reaction.UserId && x.OwnerGiveawayId == validGiveaway.Id)) { return; } User user = await DatabaseQueries.GetOrCreateUserAsync(reaction.UserId); await ConsoleLogger.LogAsync("Owner-only giveaway awarding triggered for " + $"user {user.UserId}", LogLvl.DEBUG); user.Points += validGiveaway.Points; user.Experience += validGiveaway.Exp; var ownerGiveawayReaction = new OwnerGiveawayReaction { OwnerGiveawayId = validGiveaway.Id, UserId = user.UserId }; await ConsoleLogger.LogAsync("Owner-only giveaway reaction registered " + $"for user {user.UserId}.", LogLvl.DEBUG); MemoryCache.OwnerGiveawayReactions.Add(ownerGiveawayReaction); await DatabaseQueries.InsertAsync(ownerGiveawayReaction); await DatabaseQueries.UpdateAsync(user); }
public async Task Command(string duration, int points, int exp) { if (!Emote.TryParse("<:Kaguya:581581938884608001>", out Emote reactionEmote)) { throw new Exception("Emote could not be parsed."); } bool pointsGiveaway = points > 0; bool expGiveaway = exp > 0; StringBuilder titleSb = TitleStringBuilder(pointsGiveaway, expGiveaway); StringBuilder descriptionSb = DescriptionStringBuilder(duration, pointsGiveaway, expGiveaway, points, exp, out TimeSpan giveawayTimespan); await Context.Message.DeleteAsync(); // Delete message from bot owner. var embed = new KaguyaEmbedBuilder(EmbedColor.GOLD) { Title = titleSb.ToString(), Description = descriptionSb.ToString() }; RestUserMessage embedMsg = await SendEmbedAsync(embed); await embedMsg.AddReactionAsync(reactionEmote); var giveawayObj = new OwnerGiveaway { MessageId = embedMsg.Id, ChannelId = embedMsg.Channel.Id, Exp = exp, Points = points, Expiration = DateTime.Now.AddSeconds(giveawayTimespan.TotalSeconds).ToOADate(), HasExpired = false }; int id = await DatabaseQueries.InsertWithIdentityAsync(giveawayObj); OwnerGiveaway cachedObj = giveawayObj; cachedObj.Id = id; MemoryCache.OwnerGiveawaysCache.Add(cachedObj); await ConsoleLogger.LogAsync($"Owner giveaway created.", LogLvl.DEBUG); }