コード例 #1
0
ファイル: Statistics.cs プロジェクト: Biribirun/Koneko.Bot
        public async Task AddPoints(CommandContext context)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.User.IsBot)
            {
                return;
            }

            var   GuildId = context.Guild.Id;
            var   UserId  = context.User.Id;
            ulong Points  = (ulong)_rand.Next(10, 20);

            var statistics = await _statisticsRepository.GetUserStatistics(GuildId, UserId);

            if (statistics is null)
            {
                statistics = new UserStatistic()
                {
                    UserId            = UserId,
                    GuildId           = GuildId,
                    Score             = Points,
                    LastScoredMessage = DateTime.Now
                };
                await _statisticsRepository.AddUserStatistic(statistics);
            }
            else
            {
                if ((DateTime.Now - statistics.LastScoredMessage).Minutes < 2)
                {
                    return;
                }
                statistics.Score += Points;
                await _statisticsRepository.UpdateStatistics(statistics);
            }

            var user = (context.User as SocketGuildUser);

            var roles = await _statisticsRepository.GetRankRewards(GuildId);

            var userRoles = (
                from reward in roles
                join userRole in context.Guild.Roles on reward.RoleId equals userRole.Id
                where reward.ReqScore <= statistics.Score
                orderby reward.ReqScore
                select userRole).ToArray();

            if (userRoles.Length <= 0)
            {
                return;
            }

            IEnumerable <IRole> rolesToRemove = userRoles[0..^ 1];
コード例 #2
0
        public async Task Top(int page = 1)
        {
            page--;

            var currentUserStatistics = await _statisticsRepository.GetUserStatistics(Context.Guild.Id, Context.User.Id);

            var pagedUsers = await _statisticsRepository.GetStatistics(Context.Guild.Id, page);

            var totalUsers = (Context.Guild as SocketGuild).MemberCount;

            var guildMembers = await Context.Guild.GetUsersAsync();


            var formatFields = from sUser in pagedUsers
                               join gUser in guildMembers on sUser.UserId equals gUser.Id into users
                               from gUser in users.DefaultIfEmpty()
                               select new string[2] {
                gUser == null ? $"Użytkownik opuścił serwer ID: {sUser.UserId}" : Formatters.GetUserName(gUser), sUser.Score.ToString()
            };

            if (!formatFields.Any())
            {
                await ReplyImage(description : $"Brak użytkowników na pozycjach {page * 10 + 1} - {page * 10 + 10}");

                return;
            }

            int rownum = 1;

            var lines = from field in formatFields
                        select string.Format("[{0}]", (page * 10) + rownum ++).PadRight(12) + "> " +
                        $"#{field[0]}\n" +
                        $"Całkowity wynik:".PadLeft(32) + $" {field[1]}";

            var sb = new StringBuilder();

            sb.Append($"Ranking użytkowników serwera {Context.Guild.Name}\n\n");
            sb.Append(string.Join("\n", lines));
            sb.Append('\n');
            sb.Append(string.Concat(Enumerable.Repeat('-', 40)));
            //sb.Append($"\nTwoje miejsce w rankingu: {guildUsers.TakeWhile(x => x.UserId != Context.User.Id).Count() + 1} / {totalUsers}\n");
            sb.Append($"Całkowity wynik: {currentUserStatistics.Score}");
            await ReplyImage(description : $"```CS\n{sb.ToString()}```");
        }