public async Task <GlobalLeaderboard> GetGlobalLeaderboard() { try { using (var soraContext = new SoraContext()) { var users = soraContext.Users.ToList(); var sorted = users.OrderByDescending(x => x.Exp).ToList(); var resp = new GlobalLeaderboard() { ShardId = _client.ShardId }; for (int i = 0; i < (sorted.Count > 150 ? 150 : sorted.Count); i++) { var guser = sorted[i]; IUser user = _client.GetUser(guser.UserId) ?? await _restClient.GetUserAsync(guser.UserId) as IUser; if (user == null) { continue; } resp.Ranks.Add(new GuildRank() { Rank = i + 1, AvatarUrl = user.GetAvatarUrl() ?? "https://i.imgur.com/PvYs6dc.png", Discrim = user.Discriminator, Exp = (int)guser.Exp, Name = user.Username, NextExp = ExpService.CalculateNeededExp(ExpService.CalculateLevel(guser.Exp) + 1), UserId = user.Id + "" }); if (resp.Ranks.Count >= 100) { break; } } return(resp); } } catch (Exception e) { Console.WriteLine(e); } return(null); }
public ExpModule(ExpService service) { _service = service; }
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 CommandHandler(IServiceProvider provider, DiscordSocketClient client, CommandService commandService, AfkService afkService, RatelimitingService ratelimitingService, StarboardService starboardService, SelfAssignableRolesService selfService, AnnouncementService announcementService, ModService modService, GuildCountUpdaterService guildUpdate, ExpService expService, BanService banService, InteractionsService interactionsService) { _client = client; _commands = commandService; _afkService = afkService; _services = provider; _ratelimitingService = ratelimitingService; _starboardService = starboardService; _selfAssignableRolesService = selfService; _announcementService = announcementService; _modService = modService; _guildCount = guildUpdate; _banService = banService; _interactionsService = interactionsService; _guildCount.Initialize(client.ShardId, Utility.TOTAL_SHARDS, client.Guilds.Count); _client.MessageReceived += HandleCommandsAsync; //_client.MessageReceived += _afkService.Client_MessageReceived; _commands.Log += CommandsOnLog; _client.JoinedGuild += ClientOnJoinedGuild; _client.LeftGuild += ClientOnLeftGuild; _client.MessageReceived += expService.IncreaseEpOnMessageReceive; _client.ReactionAdded += _starboardService.ClientOnReactionAdded; _client.ReactionRemoved += _starboardService.ClientOnReactionRemoved; _client.UserJoined += _selfAssignableRolesService.ClientOnUserJoined; _client.UserJoined += _announcementService.ClientOnUserJoined; _client.UserLeft += _announcementService.ClientOnUserLeft; //mod Service _client.UserBanned += _modService.ClientOnUserBanned; _client.UserUnbanned += _modService.ClientOnUserUnbanned; }
public async Task <GuildLeaderboard> GetGuildLeaderboard(ulong guildId) { try { using (var soraContext = new SoraContext()) { var guild = _client.GetGuild(guildId); if (guild == null) { return(new GuildLeaderboard() { Success = false }); } var resp = new GuildLeaderboard() { Success = true, AvatarUrl = guild.IconUrl ?? "https://i.imgur.com/PvYs6dc.png", GuildName = guild.Name }; var levelRoles = soraContext.GuildLevelRoles.Where(x => x.GuildId == guildId).ToList(); var guildUsers = soraContext.GuildUsers.Where(x => x.GuildId == guildId).ToList(); var sorted = guildUsers.OrderByDescending(x => x.Exp).ToList(); var sortedLvls = levelRoles.OrderBy(x => x.RequiredLevel).ToList(); for (int i = 0; i < sortedLvls.Count; i++) { var role = sortedLvls[i]; var r = guild.GetRole(role.RoleId); if (r == null || role.Banned) { continue; } resp.RoleRewards.Add(new RoleReward() { Name = r.Name, Color = $"rgb({r.Color.R}, {r.Color.G}, {r.Color.B})", LevelReq = role.RequiredLevel }); } int rank = 1; for (int i = 0; i < sorted.Count; i++) { var user = sorted[i]; if (rank > 100) { break; } IUser u = _client.GetUser(user.UserId) ?? await _restClient.GetUserAsync(user.UserId) as IUser; if (u == null) { continue; } resp.Ranks.Add(new GuildRank() { AvatarUrl = u.GetAvatarUrl() ?? "https://i.imgur.com/PvYs6dc.png", Discrim = u.Discriminator, Exp = (int)user.Exp, Name = u.Username, NextExp = ExpService.CalculateNeededExp(ExpService.CalculateLevel(user.Exp) + 1), Rank = rank }); rank++; } return(resp); } } catch (Exception e) { Console.WriteLine(e); } return(new GuildLeaderboard() { Success = false }); }
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); }