public static async Task AddGuildUser(ulong UserId, ulong GuildId, DiscordGuildUser.PermissionLevels permission) { using (var DbContext = new SQLiteDatabaseContext()) { var query = DbContext.DiscordUsersDB.Where(x => x.UserId == UserId && x.GuildId == GuildId); if (query.Count() < 1) { var user = new DiscordGuildUser { UserId = UserId, GuildId = GuildId, PermissionLevel = permission }; DbContext.DiscordUsersDB.Add(user); await DbContext.SaveChangesAsync(); } else { var user = query.FirstOrDefault(); if (user.PermissionLevel != permission) { user.PermissionLevel = permission; DbContext.DiscordUsersDB.Update(user); await DbContext.SaveChangesAsync(); } } } }
public async Task GetCoinAmount([Summary("The @user you want to check. Leave blank to get your own balance")] DiscordGuildUser userT = null) { var user = userT?.GuildUser ?? (IGuildUser)Context.User; // We dont care if the user exists. So we take the easy way out var amount = _coinRepo.GetCoins(user.Id); await ReplyAsync("", embed : SimpleEmbed(Blue, $"💰 {(user.Id == Context.User.Id ? "You have" : $"{Formatter.UsernameDiscrim(user)} has")} {amount.ToString()} Sora Coins.") .Build()); }
public static async Task RemoveGuildUser(DiscordGuildUser user) { using (var DbContext = new SQLiteDatabaseContext()) { DbContext.DiscordUsersDB.RemoveRange( DbContext.DiscordUsersDB.Where(x => x.UserId == user.UserId && x.GuildId == user.GuildId )); await DbContext.SaveChangesAsync(); } }
public async Task Marry(DiscordGuildUser user) { await ReplyEmbed( $"{Formatter.UsernameDiscrim(user.GuildUser)}, do you want to marry {Formatter.UsernameDiscrim(Context.User)}?", Purple, "💍"); var criteria = InteractiveServiceExtensions.CreateEnsureFromUserInChannelCriteria(user.GuildUser.Id, Context.Channel.Id); var resp = await _interactiveService.NextMessageAsync(Context, criteria, TimeSpan.FromSeconds(45)).ConfigureAwait(false); if (resp == null) { await ReplyFailureEmbed($"{Formatter.UsernameDiscrim(user.GuildUser)} didn't answer in time >.<"); return; } if (!InteractiveServiceExtensions.StringContainsYes(resp.Content)) { await ReplyFailureEmbed($"{Formatter.UsernameDiscrim(user.GuildUser)} didn't answer with a yes ˚‧º·(˚ ˃̣̣̥᷄⌓˂̣̣̥᷅ )‧º·˚"); return; } var res = await _marriageRepo.TryAddMarriage(Context.User.Id, user.GuildUser.Id); if (!res) { await ReplyFailureEmbed(res.Err().Message); return; } var eb = new EmbedBuilder() { Color = Purple, Title = "💑 You are now married", ImageUrl = "https://media.giphy.com/media/iQ5rGja9wWB9K/giphy.gif" }; await ReplyEmbed(eb); }
private async Task GetAllMarriagesAdv(DiscordGuildUser userT = null, bool adv = false) { var user = userT?.GuildUser ?? (IGuildUser)Context.User; var marriages = await _marriageRepo.GetAllMarriagesOfUser(user.Id); if (!marriages) { await ReplyFailureEmbed($"{Formatter.UsernameDiscrim(user)} has no marriages"); return; } var eb = new EmbedBuilder() { Color = Purple, Title = $"💕 Marriages of {Formatter.UsernameDiscrim(user)}", ThumbnailUrl = user.GetAvatarUrl() ?? user.GetDefaultAvatarUrl(), Footer = RequestedByMe() }; var marr = marriages.Some(); foreach (var marriage in marr) { var partnerId = marriage.Partner1Id == user.Id ? marriage.Partner2Id : marriage.Partner1Id; var u = await _userService.GetOrSetAndGet(partnerId); var name = u ? Formatter.UsernameDiscrim((~u)) : partnerId.ToString(); eb.AddField(x => { x.IsInline = true; x.Name = name; x.Value = $"*Since {marriage.PartnerSince:dd/MM/yyyy}*{(adv ? $"\nID: {partnerId.ToString()}" : "")}"; }); } await ReplyEmbed(eb); }
public async Task DivorceMention(DiscordGuildUser user) { await this.Divorce(user.GuildUser.Id); }
public async Task GetAllMarriagesExt(DiscordGuildUser userT = null) { await this.GetAllMarriagesAdv(userT, true); }
public async Task GetWaifuStats( [Summary("@User to get the stats to. Leave blank to get your own")] DiscordGuildUser userT = null) { var user = userT?.GuildUser ?? (IGuildUser)Context.User; var waifus = await _waifuService.GetAllWaifusFromUser(user.Id).ConfigureAwait(false); if (waifus == null || waifus.Count == 0) { await ReplyFailureEmbed($"{(userT == null ? "You have" : $"{Formatter.UsernameDiscrim(user)} has")} no waifus."); return; } // Unlike the total waifus we actually cache this value for 1 hour. This is because it's less important and can be inaccurate for a while // This frees up resources for more important tasks var allRarityStats = await _waifuService.GetTotalWaifuRarityStats().ConfigureAwait(false); var eb = new EmbedBuilder() { Color = Purple, Footer = RequestedByFooter(Context.User), ThumbnailUrl = user.GetAvatarUrl() ?? user.GetDefaultAvatarUrl(), Title = "Waifu Stats", Description = "This shows you how many waifus you own of each category and your completion percentage." + " This does not take dupes into account!" }; int totalHas = 0; var userRarity = waifus.GroupBy(w => w.Rarity, (rarity, ws) => new { rarity, count = ws.Count() }) .ToDictionary(x => x.rarity, x => x.count); // This line kinda sucks. Don't know if there is a better way to solve this but that aint it chief var allRarities = ((WaifuRarity[])Enum.GetValues(typeof(WaifuRarity))).OrderBy(x => x); int total = 0; foreach (var rarity in allRarities) { userRarity.TryGetValue(rarity, out int count); int allRar = allRarityStats[rarity]; total += allRar; eb.AddField(x => { x.Name = WaifuFormatter.GetRarityString(rarity); x.IsInline = true; x.Value = $"{count.ToString()} / {allRar.ToString()} ({((float) count / allRar):P2})"; }); totalHas += count; } eb.AddField(x => { x.Name = "Total"; x.IsInline = true; x.Value = $"{totalHas.ToString()} / {total.ToString()} ({((float) totalHas / total):P2})"; }); await ReplyAsync("", embed : eb.Build()); }
public async Task GenerateImage( [Summary("@User or leave blank to get your own")] DiscordGuildUser userT = null) { var user = userT?.GuildUser ?? (IGuildUser)Context.User; var userStatsM = await _profileRepo.GetProfileStatistics(user.Id, Context.Guild.Id).ConfigureAwait(false); if (!userStatsM.HasValue) { await ReplyFailureEmbed( $"{Formatter.UsernameDiscrim(user)} is not in my Database :/ Make sure he used or chatted with Sora at least once."); return; } try { // First get his avatar await _hch.DownloadAndSaveFile( new Uri(user.GetAvatarUrl() ?? user.GetDefaultAvatarUrl()), Path.Combine(_imgGen.ImageGenPath, ImageGenerator.AVATAR_CACHE, $"{user.Id.ToString()}.png")) .ConfigureAwait(false); // Now generate image string filePath = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.PROFILE_CARDS, $"{user.Id.ToString()}.png"); var us = ~userStatsM; var globalLevel = ExpService.CalculateLevel(us.GlobalExp); var localLevel = ExpService.CalculateLevel(us.LocalExp); _imgGen.GenerateProfileImage(new ProfileImageGenDto() { UserId = user.Id, Name = user.Username, GlobalExp = us.GlobalExp, GlobalLevel = globalLevel, GlobalRank = us.GlobalRank, GlobalNextLevelExp = ExpService.CalculateNeededExp(globalLevel + 1), HasCustomBg = us.HasCustomBg, LocalExp = us.LocalExp, LocalRank = us.LocalRank, LocalLevel = localLevel, LocalNextLevelExp = ExpService.CalculateNeededExp(localLevel + 1), ClanName = us.ClanName }, filePath); await Context.Channel.SendFileAsync(filePath); } catch (Exception e) { await ReplyFailureEmbedExtended( "Failed to generate image. Something went wrong sorry :/", "This could have multiple reasons. One of them could be that your username has characters that " + "are currently not supported. This is any weird character that you wouldn't naturally find on your standard " + "keyboard."); if (e.InnerException is NotImplementedException) { return; // Since we dont care about exceptions about not supported text and shit. Can't do anything about that } _log.LogError(e, $"Failed to generate image for {user.Id.ToString()} ({user.Username})"); } finally { // Remove avatar string avatar = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.AVATAR_CACHE, $"{user.Id.ToString()}.png"); if (File.Exists(avatar)) { File.Delete(avatar); } // Remove profile image string profileImg = Path.Combine(_imgGen.ImageGenPath, ImageGenerator.PROFILE_CARDS, $"{user.Id.ToString()}.png"); if (File.Exists(profileImg)) { File.Delete(profileImg); } } }
public async Task UserInfo( [Summary("@User to get info about. Mention no one to get info about yourself")] DiscordGuildUser userT = null) { var user = userT?.GuildUser ?? (IGuildUser)Context.User; var userDb = await _userRepo.GetUser(user.Id); var footer = RequestedByMe(); var eb = new EmbedBuilder() { Color = Blue, ThumbnailUrl = user.GetAvatarUrl() ?? user.GetDefaultAvatarUrl(), Title = $"{INFO_EMOJI} {Formatter.UsernameDiscrim(user)}", Footer = footer.WithText($"{footer.Text} | ID: {user.Id.ToString()}"), }; eb.AddField(x => { x.Name = "Joined Discord"; x.IsInline = false; x.Value = $"On {user.CreatedAt.ToString("dd/MM/yyyy")}. " + $"That is {((int) DateTime.Now.Subtract(user.CreatedAt.DateTime).TotalDays).ToString()} days ago!"; }); eb.AddField(x => { string joined = "_Unknown_"; if (user.JoinedAt != null) { joined = $"On {user.JoinedAt.Value.DateTime.ToString("dd/MM/yyyy")}. " + $"That is {((int) DateTime.Now.Subtract(user.JoinedAt.Value.DateTime).TotalDays).ToString()} days ago!"; } x.Name = "Joined Server"; x.IsInline = false; x.Value = joined; }); eb.AddField(x => { x.Name = "Nickname"; x.IsInline = true; x.Value = string.IsNullOrWhiteSpace(user.Nickname) ? "_none_" : user.Nickname; }); eb.AddField(x => { x.Name = "Avatar"; x.IsInline = true; x.Value = $"[Click Here]({(user.GetAvatarUrl(ImageFormat.Auto, 1024) ?? user.GetDefaultAvatarUrl())})"; }); eb.AddField(x => { string roles = String.Join(", ", Context.Guild.Roles .Where(r => user.RoleIds.Any(id => id == r.Id) && !r.IsEveryone) .Select(r => r.Name)); x.Name = "Roles"; x.IsInline = true; x.Value = string.IsNullOrWhiteSpace(roles) ? "_none_" : roles; }); var coins = userDb?.Coins ?? 0; eb.AddField(x => { x.Name = "Sora Coins"; x.IsInline = true; x.Value = $"{coins.ToString()} SC"; }); uint exp = userDb?.Exp ?? 0; int lvl = ExpService.CalculateLevel(exp); eb.AddField(x => { x.Name = "EXP"; x.IsInline = true; x.Value = exp.ToString(); }); eb.AddField(x => { x.Name = "Level"; x.IsInline = true; x.Value = lvl.ToString(); }); var waifu = userDb?.FavoriteWaifu; if (waifu != null) { eb.AddField(x => { x.IsInline = false; x.Name = "Favorite Waifu"; x.Value = waifu.Name; }); eb.ImageUrl = waifu.ImageUrl; } await ReplyEmbed(eb); }