コード例 #1
0
ファイル: Leaderboard.cs プロジェクト: glen3b/DiscordBot
        private char GetDifferenceChar(ulong id, bool user)
        {
            int index;
            int oldIndex;

            if (user)
            {
                index    = OrderedUserMessages.FindIndex(x => x.Key == id);
                oldIndex = OldLeaderboard.OrderedUserMessages.FindIndex(x => x.Key == id);
            }
            else
            {
                index    = OrderedChannelMessages.FindIndex(x => x.Key == id);
                oldIndex = OldLeaderboard.OrderedChannelMessages.FindIndex(x => x.Key == id);
            }

            return(oldIndex < 0 ? '!' : (oldIndex < index ? '-' : (oldIndex == index ? '~' : '+')));
        }
コード例 #2
0
ファイル: Leaderboard.cs プロジェクト: glen3b/DiscordBot
        public async Task <List <string> > ToStringsAsync(bool combine = false)
        {
            IGuild        guild    = bot.Client.GetGuild(GuildId);
            List <string> messages = new List <string>();

            var builder = new StringBuilder($"**Messages Leaderboard**\n");

            switch (Type)
            {
            case LeaderboardType.Full:
            case LeaderboardType.Delta:
                builder.AppendLine("For messages sent from the beginning of time");
                break;

            case LeaderboardType.Today:
                builder.AppendLine("For messages since midnight PT");
                break;

            case LeaderboardType.Custom:
            case LeaderboardType.Past24Hours:
                builder.AppendLine($"For messages in the last {TimePeriod.ToLongString()}");
                break;
            }

            builder.AppendLine($"```diff\n{(OrderedChannelMessages.Count(channel => ChannelLookup.ContainsKey(channel.Key)) > 25 ? "Top 25 " : "")}Channels");
            foreach (var channel in OrderedChannelMessages.Where(channel => ChannelLookup.ContainsKey(channel.Key)).Take(25))
            {
                if (OldLeaderboard == null)
                {
                    builder.AppendFormat("{0,-7}({1,4:0.0}%)   #{2}\n", channel.Value, channel.Value / (double)TotalMessages * 100, ChannelLookup.TryGetValue(channel.Key, out string channelName) ? channelName : "<deleted channel>");
                }
                else
                {
                    builder.AppendFormat("{5}  {0,-7} ({3:+;-}{3,4:###0;###0}) {1,9:0.00%} ({4,7:+00.00%;-00.00%})   #{2}\n", channel.Value, channel.Value / (double)TotalMessages, ChannelLookup.TryGetValue(channel.Key, out string channelName) ? channelName : "<deleted channel>", CalculateMessageDifference(channel.Key, false), CalculatePercentageDifference(channel.Key, false), GetDifferenceChar(channel.Key, false));
                }
            }
            if (!combine)
            {
                builder.AppendLine("```");
                messages.Add(builder.ToString());
                builder.Clear();
                builder.AppendLine("```diff");
            }
            builder.AppendLine($"\n{(OrderedUserMessages.Count > 25 ? "Top 25 " : "")}Users");
            foreach (var user in OrderedUserMessages.Take(25))
            {
                if (OldLeaderboard == null)
                {
                    builder.AppendFormat("{0,-7}({1,4:0.0}%)   {2}\n", user.Value, user.Value / (double)TotalMessages * 100, UserLookup.TryGetValue(user.Key, out string username) ? username : (await guild.GetUserAsync(user.Key))?.NicknameOrUsername() ?? (await bot.RestClient.GetUserAsync(user.Key))?.Username ?? "<unknown user>");
                }
                else
                {
                    builder.AppendFormat("{5}  {0,-7} ({3:+;-}{3,4:###0;###0}) {1,9:0.00%} ({4,7:+00.00%;-00.00%})   {2}\n", user.Value, user.Value / (double)TotalMessages, UserLookup.TryGetValue(user.Key, out string username) ? username.Replace("```", "`​`​`​") : (await guild.GetUserAsync(user.Key))?.NicknameOrUsername().Replace("```", "`​`​`​") ?? (await bot.RestClient.GetUserAsync(user.Key))?.Username.Replace("```", "`​`​`​") ?? "<unknown user>", CalculateMessageDifference(user.Key, true), CalculatePercentageDifference(user.Key, true), GetDifferenceChar(user.Key, true));
                }
            }
            if (OldLeaderboard == null)
            {
                builder.AppendLine($"\nTotal messages in server: {TotalMessages}");
            }
            else
            {
                switch (Type)
                {
                case LeaderboardType.Full:
                case LeaderboardType.Delta:
                    builder.AppendLine($"\nTotal messages in server: {TotalMessages} ({TotalMessages - OldLeaderboard.TotalMessages:+#;-#;+0})\n");
                    builder.AppendLine($"Changes from {(TimeGenerated - OldLeaderboard.TimeGenerated).ToLongString()} ago");
                    break;

                case LeaderboardType.Today:
                    builder.AppendLine($"\nTotal messages sent today: {TotalMessages} ({TotalMessages - OldLeaderboard.TotalMessages:+#;-#;+0})\n");
                    builder.AppendLine("All current values since midnight PT, delta values are comparisons from the previous day");
                    break;

                case LeaderboardType.Custom:
                case LeaderboardType.Past24Hours:
                    builder.AppendLine($"\nTotal messages sent in the last {TimePeriod.ToLongString()}: {TotalMessages} ({TotalMessages - OldLeaderboard.TotalMessages:+#;-#;+0})\n");
                    builder.AppendLine($"All current values since {TimePeriod.ToLongString()} ago, delta values are comparisons from the previous {TimePeriod.ToLongString()}");
                    break;
                }
            }
            builder.AppendLine($"Generated {TimeZoneInfo.ConvertTimeBySystemTimeZoneId(TimeGenerated, bot.DefaultTimeZone):f}");
            builder.Append("```");
            messages.Add(builder.ToString());

            return(messages);
        }