Exemple #1
0
        public static List <string> FormatEmoteStats(List <EmoteStats> emoteStats)
        {
            var rank = 1;

            var textLines = new List <string>();

            foreach (var emoteUsage in emoteStats)
            {
                var emoteCode = EmoteHelper.BuildEmoteCode(emoteUsage.EmoteId, emoteUsage.IsAnimated);

                textLines.Add($"{rank}.{emoteCode}:{emoteUsage.Usage}");

                rank++;
            }

            return(textLines);
        }
        public static List <string> FormatEmoteStats(List <EmoteStats> emoteStats, int rankOffset)
        {
            var rankPadding  = emoteStats.Count.ToString().Length;
            var largestUsage = emoteStats.Max(e => e.Usage);
            var usagePadding = largestUsage.ToString().Length;

            var rank = 1 + rankOffset;

            var textLines = new List <string>();

            foreach (var emoteUsage in emoteStats)
            {
                var formattedRank  = rank.ToString().PadLeft(rankPadding, ' ');
                var formattedUsage = emoteUsage.Usage.ToString().PadLeft(usagePadding, ' ');

                var emoteCode = EmoteHelper.BuildEmoteCode(emoteUsage.EmoteId, emoteUsage.IsAnimated);

                textLines.Add($"{formattedRank}.`{emoteCode}`:{formattedUsage}");

                rank++;
            }

            return(textLines);
        }
        public async Task GetEmojiCode(CommandContext ctx, DiscordEmoji emoji)
        {
            var isUnicodeEmoji = emoji.Id == 0;

            if (isUnicodeEmoji)
            {
                var unicodeEncoding = new UnicodeEncoding(true, false);

                var bytes = unicodeEncoding.GetBytes(emoji.Name);

                var sb = new StringBuilder();
                for (int i = 0; i < bytes.Length; i++)
                {
                    sb.AppendFormat("{0:X2}", bytes[i]);
                }

                var bytesAsString = sb.ToString();

                var formattedSb = new StringBuilder();

                for (int i = 0; i < sb.Length; i += 4)
                {
                    formattedSb.Append($"\\u{bytesAsString.Substring(i, 4)}");
                }

                var result = formattedSb.ToString();

                await ctx.RespondAsync($"`{result}`");
            }
            else
            {
                var result = EmoteHelper.BuildEmoteCode(emoji.Id, emoji.Name, emoji.IsAnimated);

                await ctx.RespondAsync($"`{result}`");
            }
        }
            protected override async Task Handle(GetUserEmoteStatsRequest request, CancellationToken cancellationToken)
            {
                var ctx      = request.Ctx;
                var page     = request.Page;
                var fromDate = request.FromDate;
                var userId   = request.UserId;
                var mention  = request.Mention;

                var guildId = ctx.Guild.Id;

                int pageOffset = page - 1;

                var emoteUsageResult = await _emoteStatsRepo.GetBestEmotesForUser(guildId, userId, pageOffset, ResultsPerPage, fromDate);

                var emoteUsageItems = emoteUsageResult.Items;
                var safePageOffset  = emoteUsageResult.PageOffset;
                var pageCount       = emoteUsageResult.PageCount;
                var totalCount      = emoteUsageResult.TotalCount;

                var emoteUsageList = emoteUsageItems
                                     .Select(r => new ExtendedEmoteStats()
                {
                    EmoteCode         = EmoteHelper.BuildEmoteCode(r.EmoteId, r.IsAnimated),
                    Usage             = r.Usage,
                    PercentageOfTotal = (double)r.Usage / r.TotalUsage
                })
                                     .ToList();

                if (emoteUsageList.Count == 0)
                {
                    await ctx.RespondAsync("No stats yet");

                    return;
                }

                string rangeText;

                if (safePageOffset == 0)
                {
                    rangeText = ResultsPerPage.ToString();
                }
                else
                {
                    var rangeMin = ResultsPerPage * safePageOffset + 1;
                    var rangeMax = Math.Min(ResultsPerPage * safePageOffset + ResultsPerPage, totalCount);
                    rangeText = $"{rangeMin} - {rangeMax}";
                }

                var title = $"Top {rangeText} emotes for {mention}";

                if (fromDate != null)
                {
                    title += $" from {fromDate.Value:yyyy-MM-dd}";
                }

                var rankOffset = safePageOffset * ResultsPerPage;

                var lines = FormattedResultHelper.FormatExtendedEmoteStats(emoteUsageList, rankOffset);

                var body = FormattedResultHelper.BuildBody(lines);

                var result = new StringBuilder();

                result.AppendLine(title);
                result.AppendLine();
                result.AppendLine(body);

                if (pageCount > 1)
                {
                    result.AppendLine();

                    var pagingText = $"Page {safePageOffset + 1}/{pageCount}";

                    if (safePageOffset + 1 < pageCount)
                    {
                        pagingText += $" (use -p {safePageOffset + 2} to view next page)";
                    }

                    result.AppendLine(pagingText);
                }

                var formattedResultMessage = $"`{result}`";

                await ctx.RespondAsync(formattedResultMessage);
            }
Exemple #5
0
            protected override async Task Handle(GetEmoteStatsRequest request, CancellationToken cancellationToken)
            {
                var ctx      = request.Ctx;
                var emote    = request.Emote;
                var fromDate = request.FromDate;

                var guildId = ctx.Guild.Id;

                var emoteUsageResult = await _emoteStatsRepo.GetSingleEmoteStats(guildId, emote, MaxUserCount, fromDate);

                if (emoteUsageResult == null)
                {
                    await ctx.RespondAsync("No stats yet");

                    return;
                }

                var emoteStats = emoteUsageResult.EmoteStats;
                var emoteUsers = emoteUsageResult.EmoteUsers;

                var emoteCode  = EmoteHelper.BuildEmoteCode(emoteStats.EmoteId, emoteStats.IsAnimated);
                var totalUsage = emoteStats.Usage;

                var title = $"Stats for `{emoteCode}`";

                if (fromDate != null)
                {
                    title += $" from {fromDate.Value:yyyy-MM-dd}";
                }

                var result = new StringBuilder();

                result.AppendLine(title);
                result.AppendLine($"Total usage: {totalUsage}");

                if (emoteUsers.Count > 0)
                {
                    var extendedEmoteUsers = emoteUsers
                                             .Select(r => new ExtendedEmoteUser()
                    {
                        UserId            = r.UserId,
                        Usage             = r.Usage,
                        PercentageOfTotal = (double)r.Usage / totalUsage
                    })
                                             .ToList();

                    // Resolve display names
                    foreach (var emoteUser in extendedEmoteUsers)
                    {
                        DiscordMember user;

                        if (ctx.Guild.Members.ContainsKey(emoteUser.UserId))
                        {
                            user = ctx.Guild.Members[emoteUser.UserId];
                        }
                        else
                        {
                            try
                            {
                                user = await ctx.Guild.GetMemberAsync(emoteUser.UserId);
                            }
                            catch
                            {
                                user = null !;
                            }
                        }

                        if (user != null)
                        {
                            emoteUser.DisplayName = user.GetNicknameOrUsername();
                        }
                        else
                        {
                            emoteUser.DisplayName = "Unknown user";
                        }
                    }

                    result.AppendLine();
                    result.AppendLine("Top users");

                    var lines = FormattedResultHelper.FormatExtendedEmoteUsers(extendedEmoteUsers, 0);

                    lines.ForEach(l => result.AppendLine(l));
                }

                var formattedResultMessage = $"`{result}`";

                await ctx.RespondAsync(formattedResultMessage);
            }