コード例 #1
0
        public async Task <Unit> Handle(ApplyConditionalRolesCommand request, CancellationToken cancellationToken)
        {
            var conditionalRolesEntity = await _context.ConditionalRoles
                                         .Where(r => r.GuildId == request.GuildId)
                                         .ToArrayAsync(cancellationToken);

            var conditionalRoles = conditionalRolesEntity
                                   .GroupBy(r => r.ResultRoleId)
                                   .ToDictionary(r => r.Key, r => r.Select(x => x.RequiredRoleId).ToList());

            var resultingRolesWithNoRemain = conditionalRolesEntity
                                             .Where(r => !r.Remain)
                                             .Select(r => r.ResultRoleId)
                                             .ToArray();

            foreach (var(memberId, roles) in request.Members)
            {
                foreach (var role in roles
                         .Intersect(resultingRolesWithNoRemain)
                         .Where(r => !roles.Intersect(conditionalRoles[r]).Any()))
                {
                    await _roleService.RevokeRoleAsync(request.GuildId, role, memberId, "conditional roles");
                }

                foreach (var(resultingRole, _) in conditionalRoles
                         .Where(r => !roles.Contains(r.Key))
                         .Where(r => roles.Intersect(r.Value).Any()))
                {
                    await _roleService.GrantRoleAsync(request.GuildId, resultingRole, memberId, "conditional roles");
                }
            }

            return(Unit.Value);
        }
コード例 #2
0
        public async Task ShouldAssignReactionRole_WhenAnExceptionIsThrownForSomeRoles()
        {
            var guildId   = 16;
            var channelId = 15;
            var messageId = 394;
            var emojiId   = 29;

            var badRoleId  = 500;
            var goodRoleId = 6944;

            _dbContext.ConfigureMockDbSet(x => x.ReactionRoles,
                                          new[]
            {
                CreateReactionRole(guildId, channelId, emojiId, messageId, badRoleId),
                CreateReactionRole(guildId, channelId, emojiId, messageId, goodRoleId)
            });

            var command = CreateCommand(guildId, channelId, messageId, emojiId, "TEST");

            _roleService.GrantRoleAsync(command.GuildId, (ulong)badRoleId, command.UserId, "reaction role")
            .Throws <Exception>();

            var result = await _fixture.SendAsync(command);

            result.Should().BeTrue();
            await _roleService.Received(1)
            .GrantRoleAsync(command.GuildId, (ulong)goodRoleId, command.UserId, "reaction role");

            logger.Received().Log(Arg.Is <LogLevel>(x => x == LogLevel.Error), Arg.Any <string>());

            await _roleService.DidNotReceive()
            .RevokeRoleAsync(Arg.Any <ulong>(), Arg.Any <ulong>(), Arg.Any <ulong>(), Arg.Any <string>());
        }
コード例 #3
0
        public async Task <Unit> Handle(ApplyJoinRolesCommand request, CancellationToken cancellationToken)
        {
            foreach (var guildId in _discordGuildService.GetAllCurrentGuildIds())
            {
                var joinRoles = await _applicationDbContext.JoinRoles
                                .Where(r => r.GuildId == guildId)
                                .ToListAsync(cancellationToken);

                foreach (var guildMember in _discordGuildMemberService.GetGuildMembers(guildId))
                {
                    if (guildMember.IsPending)
                    {
                        continue;
                    }

                    foreach (var r in joinRoles)
                    {
                        try
                        {
                            await _discordGuildRoleService.GrantRoleAsync(guildId,
                                                                          r.RoleId,
                                                                          guildMember.Id,
                                                                          "join role");
                        }
                        catch (Exception ex)
                        {
                            _logger.LogError(ex, "couldn't grant role to user");
                        }
                    }
                }
            }

            return(Unit.Value);
        }
コード例 #4
0
        public async Task <Unit> Handle(MuteCommand request, CancellationToken cancellationToken)
        {
            await _roleService.GrantRoleAsync(request.GuildId, _roleConfiguration.MuteRoleId, request.UserId, "user muted for " + request.TimeSpan.Humanize());

            await AddActiveMute(request, cancellationToken);
            await AddActionLog(request, cancellationToken);

            var message = $"You have been muted for {request.TimeSpan.Humanize()}.\n\nAdditional comments: {request.Comments}";
            await _directMessageService.SendMessageAsync(request.GuildId, request.UserId, message);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
コード例 #5
0
        public async Task <bool> Handle(ApplyReactionRolesCommand request, CancellationToken cancellationToken)
        {
            var roles = await _context.ReactionRoles
                        .Where(x => x.GuildId == request.GuildId && x.ChannelId == request.ChannelId &&
                               x.MessageId == request.MessageId)
                        .ToListAsync(cancellationToken);

            roles = roles.Where(r => r.EmoteId == EmoteId(request.Emoji)).ToList();

            foreach (var reactionRole in roles)
            {
                try
                {
                    if (request.Add)
                    {
                        await _roleService.GrantRoleAsync(request.GuildId, reactionRole.RoleId, request.UserId,
                                                          "reaction role");

                        _logger.LogInformation("Granted Role for {@RoleAction}",
                                               new { request.GuildId, reactionRole.RoleId, request.UserId });
                    }
                    else
                    {
                        await _roleService.RevokeRoleAsync(request.GuildId, reactionRole.RoleId, request.UserId,
                                                           "reaction role");

                        _logger.LogInformation("Revoked Role for {@RoleAction}",
                                               new { request.GuildId, reactionRole.RoleId, request.UserId });
                    }
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Couldn't alter reaction role. Request: {@Request} Role: {@Role}", request,
                                     reactionRole);
                }
            }

            return(true);
        }
コード例 #6
0
        public async Task <Unit> Handle(ApplyLevelRolesCommand request, CancellationToken cancellationToken)
        {
            var levelRoles = await _context.LevelRoles
                             .Where(r => r.GuildId == request.GuildId)
                             .Where(r => r.Lvl <= request.Level)
                             .ToArrayAsync(cancellationToken);

            foreach (var roleId in levelRoles
                     .Where(r => r.Lvl == request.Level)
                     .Select(r => r.RoleId))
            {
                await _roleService.GrantRoleAsync(request.GuildId, roleId, request.MemberId, "level roles");
            }

            var groups = levelRoles
                         .Where(r => !r.Remain)
                         .GroupBy(r => r.Lvl)
                         .OrderBy(r => r.Key)
                         .ToDictionary(k => k.Key, v => v.Select(r => r.RoleId).ToArray());
            var keySet = groups.Keys.ToArray();

            for (var i = 0; i < keySet.Length - 1; i++)
            {
                if (request.Level >= keySet[i] && request.Level < keySet[i + 1])
                {
                    continue;
                }

                foreach (var role in groups[keySet[i]].Intersect(request.RoleIds))
                {
                    await _roleService.RevokeRoleAsync(request.GuildId, role, request.MemberId, "level roles");
                }
            }

            return(Unit.Value);
        }