async Task OpenInternalAsync(ulong userId) { var dbInventory = await DB.Inventory.GetAsync(userId); if (dbInventory.Spheres == 0u) { await messageService.SendMessageAsync("spheres-nospheres", Settings.ChannelId.Commands, new FormatData(userId)); return; } await DB.Inventory.RemoveAsync(userId, new InventoryData { Spheres = 1u }); OpenedSphere?.Invoke(null, new OpenedSphereEventArgs(userId)); var sphere = new SphereReward(); await rewardService.DeliverToAsync(userId, sphere); await DB.Statistics.AddAsync(userId, new StatisticData { SpheresOpened = 1u }); await messageService.SendMessageAsync("spheres-open-success", Settings.ChannelId.Commands, new FormatData(userId) { Reward = sphere }); }
async Task StartBotGifts() { RiftBot.Log.Debug("Gifts are on the way.."); var users = await DB.Cooldowns.GetBotRespectedUsersAsync(); if (users.Count == 0) { RiftBot.Log.Debug("No users with bot respect, rescheduling."); InitTimer(); return; } foreach (var userId in users) { if (!IonicHelper.GetGuildUserById(Settings.App.MainGuildId, userId, out var sgUser)) { continue; } var reward = AvailableRewards.Random(); await rewardService.DeliverToAsync(userId, reward); } await messageService.SendMessageAsync("yasuo-botrespect-success", Settings.ChannelId.Chat, null); RiftBot.Log.Debug("Finished sending gifts"); InitTimer(); }
async Task OpenInternalAsync(ulong userId, uint amount) { var dbInventory = await DB.Inventory.GetAsync(userId); if (dbInventory.Chests == 0) { await messageService.SendMessageAsync("chests-nochests", Settings.ChannelId.Commands, new FormatData(userId)); return; } if (dbInventory.Chests < amount || amount == 0) { await messageService.SendMessageAsync("chests-notenoughchests", Settings.ChannelId.Commands, new FormatData(userId)); return; } await DB.Inventory.RemoveAsync(userId, new InventoryData { Chests = amount }); ChestsOpened?.Invoke(null, new ChestsOpenedEventArgs(userId, amount)); var reward = new ChestReward(amount); await rewardService.DeliverToAsync(userId, reward); await DB.Statistics.AddAsync(userId, new StatisticData { ChestsOpened = amount }); await messageService.SendMessageAsync("chests-open-success", Settings.ChannelId.Commands, new FormatData(userId) { Reward = reward }); }
async Task GiveAsync(IUser user, ItemReward reward) { if (!(user is SocketGuildUser sgUser)) { return; } await rewardService.DeliverToAsync(sgUser.Id, reward); await messageService.SendMessageAsync("admin-give", Settings.ChannelId.Commands, new FormatData(sgUser.Id) { Reward = reward }); }
async Task GiveRewardAsync(ulong userId) { var dbReward = await DB.Rewards.GetAsync(RewardId); if (dbReward is null) { RiftBot.Log.Error($"Reward not found: ID {RewardId.ToString()}"); return; } var reward = dbReward.ToRewardBase(); await rewardService.DeliverToAsync(userId, reward); await DB.Cooldowns.SetLastDailyRewardTimeAsync(userId, DateTime.UtcNow); await messageService.SendMessageAsync("daily-success", Settings.ChannelId.Commands, new FormatData(userId) { Reward = reward }); }
async Task OpenInternalAsync(ulong userId) { var dbUserInventory = await DB.Inventory.GetAsync(userId); if (dbUserInventory.Capsules == 0u) { await messageService.SendMessageAsync("capsules-nocapsules", Settings.ChannelId.Commands, new FormatData(userId)); return; } await DB.Inventory.RemoveAsync(userId, new InventoryData { Capsules = 1u }); var reward = new CapsuleReward(); await rewardService.DeliverToAsync(userId, reward); await DB.Statistics.AddAsync(userId, new StatisticData { CapsulesOpened = 1u }); await messageService.SendMessageAsync("capsules-open-success", Settings.ChannelId.Commands, new FormatData(userId) { Reward = reward }); }
async Task GiveRewardsForLevelAsync(ulong userId, uint fromLevel, uint toLevel) { if (!IonicHelper.GetGuildUserById(Settings.App.MainGuildId, userId, out var sgUser)) { return; } var reward = new ItemReward(); for (var level = fromLevel + 1; level <= toLevel; level++) { if (level == 100u) { reward.AddCapsules(1u).AddCoins(2_000u); } else if (level == 50u) { reward.AddSpheres(1u).AddCoins(2_000u); } else if (level % 25u == 0u) { reward.AddSpheres(1u).AddCoins(2_000u); } else if (level % 10u == 0u) { reward.AddTokens(2u).AddCoins(2_000u); } else if (level % 5u == 0u) { reward.AddTickets(1u).AddCoins(2_000u); } else { reward.AddChests(1u).AddCoins(2_000u); } var nitroBooster = await DB.Roles.GetAsync(91); if (IonicHelper.HasRolesAny(sgUser, nitroBooster.RoleId)) { reward.AddChests(2u); } var rankGold = await DB.Roles.GetAsync(3); if (IonicHelper.HasRolesAny(sgUser, rankGold.RoleId)) { reward.AddCoins(250u); } var rankPlatinum = await DB.Roles.GetAsync(11); if (IonicHelper.HasRolesAny(sgUser, rankPlatinum.RoleId)) { reward.AddCoins(500u); } var rankDiamond = await DB.Roles.GetAsync(8); if (IonicHelper.HasRolesAny(sgUser, rankDiamond.RoleId)) { reward.AddCoins(750u); } var rankMaster = await DB.Roles.GetAsync(79); if (IonicHelper.HasRolesAny(sgUser, rankMaster.RoleId)) { reward.AddCoins(1000u); } var rankGrandmaster = await DB.Roles.GetAsync(71); if (IonicHelper.HasRolesAny(sgUser, rankGrandmaster.RoleId)) { reward.AddCoins(1250u); } var rankChallenger = await DB.Roles.GetAsync(23); if (IonicHelper.HasRolesAny(sgUser, rankChallenger.RoleId)) { reward.AddCoins(1500u); } } await rewardService.DeliverToAsync(userId, reward); await messageService.SendMessageAsync("levelup", Settings.ChannelId.Chat, new FormatData(userId) { Reward = reward }); }
async Task FinishAsync(RiftActiveEvent expiredEvent) { var eventLogString = $"ID {expiredEvent.Id.ToString()} \"{expiredEvent.EventName}\""; var dbEvent = await DB.Events.GetAsync(expiredEvent.EventName); if (dbEvent is null) { RiftBot.Log.Error($"Could not finish event {eventLogString}: {nameof(RiftEvent)} is null!"); return; } if (!IonicHelper.GetTextChannel(Settings.App.MainGuildId, Settings.ChannelId.Monsters, out var channel)) { RiftBot.Log.Error($"Could not finish event {eventLogString}: Event channel is null!"); return; } var message = (IUserMessage)await channel.GetMessageAsync(expiredEvent.ChannelMessageId); if (message is null) { RiftBot.Log.Error($"Could not finish event {eventLogString}: Event message is null! Deleted?"); return; } if (!IonicHelper.GetEmote(213672490491314176ul, "smite", out var emote)) { RiftBot.Log.Error($"Could not finish event {eventLogString}: Emote is null! Deleted?"); return; } // Reaction amount is limited by discord itself. // See https://discordapp.com/developers/docs/resources/channel#get-reactions var reactions = await message.GetReactionUsersAsync(emote, 100).FlattenAsync(); if (reactions is null) { RiftBot.Log.Error($"Could not finish event {eventLogString}: Unable to get reactions."); return; } var dbReward = await DB.Rewards.GetAsync(dbEvent.SharedRewardId); if (dbReward is null) { RiftBot.Log.Error($"Could not finish event {eventLogString}: " + $"Unable to get reward ID {dbEvent.SharedRewardId.ToString()}."); return; } var participants = reactions .Where(x => !x.IsBot && x.Id != IonicHelper.Client.CurrentUser.Id) .Select(x => x.Id) .ToArray(); if (participants.Length == 0) { await LogEventAsync(dbEvent.Name, null, "No reward provided ", expiredEvent.StartedBy, expiredEvent.StartedAt, dbEvent.Duration); await DB.ActiveEvents.RemoveAsync(expiredEvent.Id); RiftBot.Log.Error($"Could not finish event {eventLogString}: No participants."); return; } RiftReward specialReward = null; var specialWinnerId = 0ul; if (dbEvent.HasSpecialReward) { specialReward = await DB.Rewards.GetAsync(dbEvent.SpecialRewardId); if (specialReward is null) { RiftBot.Log.Error($"Could not finish event {eventLogString}: " + $"Unable to get special reward ID {dbEvent.SharedRewardId.ToString()}."); return; } specialWinnerId = participants.Random(); var specReward = specialReward.ToRewardBase(); await rewardService.DeliverToAsync(specialWinnerId, specReward); } var reward = dbReward.ToRewardBase(); foreach (var userId in participants) { await rewardService.DeliverToAsync(userId, reward); } await DB.ActiveEvents.RemoveAsync(expiredEvent.Id); var eventType = (EventType)dbEvent.Type; foreach (var participant in participants) { switch (eventType) { case EventType.Normal: NormalMonstersKilled?.Invoke( null, new NormalMonstersKilledEventArgs(participant, 1u)); break; case EventType.Rare: RareMonstersKilled?.Invoke( null, new RareMonstersKilledEventArgs(participant, 1u)); break; case EventType.Epic: EpicMonstersKilled?.Invoke( null, new EpicMonstersKilledEventArgs(participant, 1u)); break; } } var log = new RiftEventLog { Name = dbEvent.Name, ParticipantsAmount = (uint)participants.Length, Reward = "No reward provided", StartedBy = expiredEvent.StartedBy, StartedAt = expiredEvent.StartedAt, Duration = dbEvent.Duration, FinishedAt = DateTime.UtcNow, SpecialWinnerId = specialWinnerId }; await messageService.SendMessageAsync("event-finished", Settings.ChannelId.Monsters, new FormatData(expiredEvent.StartedBy) { EventData = new EventData { Log = log, Stored = dbEvent, } }); if (dbEvent.HasSpecialReward) { await messageService.SendMessageAsync("event-finished-special", Settings.ChannelId.Monsters, new FormatData(specialWinnerId) { Reward = specialReward.ToRewardBase() }); } await LogEventAsync(log).ConfigureAwait(false); }
async Task UpdateUsersVoiceUptimeAsync() { if (!IonicHelper.GetGuild(Settings.App.MainGuildId, out var guild)) { return; } var channels = guild.VoiceChannels .Where(x => x.CategoryId == VoiceCategoryId && x.Id != Settings.ChannelId.VoiceSetup) .ToList(); if (!channels.Any()) { return; } var users = new List <ulong>(); foreach (var channel in channels) { if (channel.Users.Count <= 1) { continue; } if (channel.UserLimit == 1) { continue; } foreach (var sgUser in channel.Users) { if (!sgUser.VoiceState.HasValue) { continue; } var voiceState = sgUser.VoiceState.Value; if (voiceState.IsMuted || voiceState.IsDeafened || voiceState.IsSelfMuted || voiceState.IsSelfDeafened) { continue; } users.Add(sgUser.Id); } } if (!users.Any()) { return; } var dbReward = await DB.Rewards.GetAsync(24); var reward = dbReward.ItemReward; foreach (var userId in users) { await rewardService.DeliverToAsync(userId, reward); await DB.Statistics.AddAsync(userId, new StatisticData { VoiceUptime = VoiceRewardsInterval }); } RiftBot.Log.Information($"Gived out voice online rewards for {users.Count.ToString()} user(s)."); }
async Task <IonicMessage> PurchaseItemInternalAsync(ulong userId, uint itemId) { var item = GetItemById(itemId); if (item is null) { return(await messageService.GetMessageAsync("store-wrongnumber", new FormatData(userId))); } if (!await CanBuyItemAsync(userId)) { return(await messageService.GetMessageAsync("itemstore-cooldown", new FormatData(userId))); } ItemReward reward; switch (item.Type) { case StoreItemType.Chest: reward = new ItemReward().AddChests(item.Amount); break; case StoreItemType.BotRespect: reward = new ItemReward().AddBotRespects(1u); break; case StoreItemType.Sphere: reward = new ItemReward().AddSpheres(1u); break; default: RiftBot.Log.Error($"Wrong type in item store: {item.Type.ToString()}!"); return(MessageService.Error); } if (!await TryWithdrawAsync(userId, item)) { switch (item.Currency) { case Currency.Coins: return(await messageService.GetMessageAsync("store-nocoins", new FormatData(userId))); case Currency.Tokens: return(await messageService.GetMessageAsync("store-notokens", new FormatData(userId))); } } if (item.Type == StoreItemType.Chest) { BoughtChests?.Invoke(null, new BoughtChestsEventArgs(userId, item.Amount)); } await rewardService.DeliverToAsync(userId, reward); await DB.Cooldowns.SetLastItemStoreTimeAsync(userId, DateTime.UtcNow); await DB.Statistics.AddAsync(userId, new StatisticData { PurchasedItems = item.Amount }); RiftBot.Log.Information($"Item purchased: #{item.Id.ToString()} by {userId.ToString()}."); return(await messageService.GetMessageAsync("store-success", new FormatData(userId) { Reward = reward })); }
async Task FinishGiveawayAsync(RiftActiveGiveaway expiredGiveaway) { var dbGiveaway = await DB.Giveaways.GetAsync(expiredGiveaway.GiveawayName); var giveawayData = $"ID {expiredGiveaway.Id.ToString()} \"{expiredGiveaway.GiveawayName}\""; if (dbGiveaway is null) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: {nameof(RiftGiveaway)} is null!"); return; } if (!IonicHelper.GetTextChannel(Settings.App.MainGuildId, Settings.ChannelId.Chat, out var channel)) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: Giveaway channel is null!"); return; } var message = (IUserMessage)await channel.GetMessageAsync(expiredGiveaway.ChannelMessageId); if (message is null) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: Giveaway message is null! Deleted?"); return; } if (!IonicHelper.GetEmote(403616665603932162, "giveaway", out var emote)) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: Emote is null! Deleted?"); return; } // Reaction amount is limited by discord itself. // See https://discordapp.com/developers/docs/resources/channel#get-reactions var reactions = await message.GetReactionUsersAsync(emote, 100).FlattenAsync(); if (reactions is null) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: Unable to get reactions."); return; } var dbReward = await DB.Rewards.GetAsync(dbGiveaway.RewardId); if (dbReward is null) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: " + $"Unable to get reward ID {dbGiveaway.RewardId.ToString()}."); return; } var reward = dbReward.ToRewardBase(); var participants = reactions .Where(x => !x.IsBot && x.Id != IonicHelper.Client.CurrentUser.Id) .Select(x => x.Id) .ToArray(); if (participants.Length == 0) { await LogGiveawayAsync(dbGiveaway.Name, null, null, "No reward provided", expiredGiveaway.StartedBy, expiredGiveaway.StartedAt, dbGiveaway.Duration); await DB.ActiveGiveaways.RemoveAsync(expiredGiveaway.Id); RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: No participants."); return; } var winners = new ulong[dbGiveaway.WinnersAmount]; if (participants.Length < dbGiveaway.WinnersAmount) { RiftBot.Log.Error($"Could not finish giveaway {giveawayData}: " + $"Not enough participants: only {participants.Length.ToString()} of minimum {dbGiveaway.WinnersAmount.ToString()}"); return; } if (participants.Length == dbGiveaway.WinnersAmount) { Array.Copy(participants, winners, dbGiveaway.WinnersAmount); } else { ulong winnerId; for (var i = 0; i < dbGiveaway.WinnersAmount; i++) { do { winnerId = participants.Random(); } while (winners.Contains(winnerId)); winners[i] = winnerId; } } foreach (var winner in winners) { await rewardService.DeliverToAsync(winner, reward); } await DB.ActiveGiveaways.RemoveAsync(expiredGiveaway.Id); foreach (var participant in participants) { GiveawaysParticipated?.Invoke(null, new GiveawaysParticipatedEventArgs(participant)); } var log = new RiftGiveawayLog { Name = dbGiveaway.Name, Winners = winners, Participants = participants, Reward = "No reward provided", StartedBy = expiredGiveaway.StartedBy, StartedAt = expiredGiveaway.StartedAt, Duration = dbGiveaway.Duration, FinishedAt = DateTime.UtcNow, }; await messageService.SendMessageAsync("giveaway-finished", Settings.ChannelId.Chat, new FormatData(expiredGiveaway.StartedBy) { Giveaway = new GiveawayData { Log = log, Stored = dbGiveaway, }, Reward = reward }); await LogGiveawayAsync(log).ConfigureAwait(false); }
async Task <IonicMessage> GiftAsync(SocketGuildUser fromSgUser, SocketGuildUser toSgUser) { var senderId = fromSgUser.Id; var receiverId = toSgUser.Id; if (toSgUser.IsBot) { RiftBot.Log.Debug("[Gift] Target is bot."); return(await messageService.GetMessageAsync("gift-target-bot", new FormatData(senderId))); } if (fromSgUser.Id == toSgUser.Id) { RiftBot.Log.Debug("[Gift] Ouch, self-gift."); return(await messageService.GetMessageAsync("gift-target-self", new FormatData(senderId))); } (var canGift, var remainingTime) = await CanGift(senderId); if (!canGift) { return(await messageService.GetMessageAsync("gift-cooldown", new FormatData(senderId) { Gift = new GiftData { Cooldown = remainingTime } })); } var dbInventory = await DB.Inventory.GetAsync(senderId); if (dbInventory.Coins < Settings.Economy.GiftPrice) { return(await messageService.GetMessageAsync("gift-nocoins", new FormatData(senderId) { Gift = new GiftData { NecessaryCoins = Settings.Economy.GiftPrice - dbInventory.Coins } })); } await DB.Inventory.RemoveAsync(senderId, new InventoryData { Coins = Settings.Economy.GiftPrice }); var giftItem = new GiftReward(); await rewardService.DeliverToAsync(receiverId, giftItem); GiftSent?.Invoke(null, new GiftSentEventArgs(senderId, receiverId)); GiftReceived?.Invoke(null, new GiftReceivedEventArgs(senderId, fromSgUser.Id)); if (toSgUser.Id == 178443743026872321ul) { GiftedFounder?.Invoke(null, new GiftedFounderEventArgs(receiverId, senderId)); } var developers = await DB.Roles.GetAsync(44); if (IonicHelper.HasRolesAny(toSgUser, developers.RoleId)) { GiftedDeveloper?.Invoke(null, new GiftedDeveloperEventArgs(senderId, receiverId)); } if (await RiftBot.IsModeratorAsync(toSgUser)) { GiftedModerator?.Invoke(null, new GiftedModeratorEventArgs(senderId, receiverId)); } if (!(await DB.Streamers.GetAsync(receiverId) is null)) { GiftedStreamer?.Invoke(null, new GiftedStreamerEventArgs(senderId, receiverId)); } RiftBot.Log.Debug("[Gift] Success."); await DB.Cooldowns.SetLastGiftTimeAsync(senderId, DateTime.UtcNow); await DB.Statistics.AddAsync(senderId, new StatisticData { GiftsSent = 1u }); await DB.Statistics.AddAsync(receiverId, new StatisticData { GiftsReceived = 1u }); return(await messageService.GetMessageAsync("gift-success", new FormatData(senderId) { Gift = new GiftData { TargetId = receiverId }, Reward = giftItem })); }
async Task GetUserBragInternalAsync(ulong userId) { if (!await CanBrag(userId)) { await messageService.SendMessageAsync("brag-cooldown", Settings.ChannelId.Commands, new FormatData(userId)); return; } var dbSummoner = await DB.LeagueData.GetAsync(userId); if (dbSummoner is null || string.IsNullOrWhiteSpace(dbSummoner.AccountId)) { await messageService.SendMessageAsync("loldata-nodata", Settings.ChannelId.Commands, new FormatData(userId)); return; } (var matchlistResult, var matchlist) = await riotService.GetLast20MatchesByAccountIdAsync(dbSummoner.SummonerRegion, dbSummoner.AccountId); if (matchlistResult != RequestResult.Success) { await messageService.SendMessageAsync("brag-nomatches", Settings.ChannelId.Commands, new FormatData(userId)); return; } (var matchDataResult, var matchData) = await riotService.GetMatchById(dbSummoner.SummonerRegion, matchlist.Random().GameId); if (matchDataResult != RequestResult.Success) { RiftBot.Log.Error("Failed to get match data"); await messageService.SendMessageAsync(MessageService.Error, Settings.ChannelId.Commands); return; } long participantId = matchData.ParticipantIdentities .First(x => x.Player.CurrentAccountId == dbSummoner.AccountId || x.Player.AccountId == dbSummoner.AccountId) .ParticipantId; var player = matchData.Participants.First(x => x.ParticipantId == participantId); if (player is null) { RiftBot.Log.Error("Failed to get player object"); await messageService.SendMessageAsync(MessageService.Error, Settings.ChannelId.Commands); return; } var champData = riotService.GetChampionById(player.ChampionId.ToString()); if (champData is null) { RiftBot.Log.Error("Failed to obtain champ data"); await messageService.SendMessageAsync(MessageService.Error, Settings.ChannelId.Commands); return; } var champThumb = riotService.GetChampionSquareByName(champData.Image); await DB.Cooldowns.SetLastBragTimeAsync(userId, DateTime.UtcNow); await DB.Statistics.AddAsync(userId, new StatisticData { BragsDone = 1u }); var win = player.Stats.Win; var reward = new ItemReward().AddRandomCoins( win ? Settings.Economy.BragWinCoinsMin : Settings.Economy.BragLossCoinsMin, win ? Settings.Economy.BragWinCoinsMax : Settings.Economy.BragLossCoinsMax); await rewardService.DeliverToAsync(userId, reward); OnUserBrag?.Invoke(null, new BragEventArgs(userId)); var queue = riotService.GetQueueNameById(matchData.QueueId); var msg = await messageService.SendMessageAsync("brag-success", Settings.ChannelId.Chat, new FormatData(userId) { Brag = new BragData { ChampionName = champData.Name, ChampionPortraitUrl = champThumb, Stats = player.Stats, QueueName = queue, }, Reward = reward }); await messageService.SendMessageAsync("brag-success-link", Settings.ChannelId.Commands, new FormatData(userId) { MessageData = new MessageData { Link = msg.GetJumpUrl() } }); }