示例#1
0
 public UnverifyService(UnverifyChecker checker, UnverifyProfileGenerator profileGenerator, UnverifyLogger logger,
                        UnverifyMessageGenerator messageGenerator, UnverifyTimeParser timeParser, BotState botState, DiscordSocketClient discord,
                        BotLoggingService loggingService, ILogger <UnverifyService> appLogger, IGrillBotRepository grillBotRepository,
                        BackgroundTaskQueue queue)
 {
     Checker = checker;
     UnverifyProfileGenerator = profileGenerator;
     UnverifyLogger           = logger;
     MessageGenerator         = messageGenerator;
     TimeParser         = timeParser;
     BotState           = botState;
     DiscordClient      = discord;
     Logger             = loggingService;
     AppLogger          = appLogger;
     GrillBotRepository = grillBotRepository;
     Queue = queue;
 }
示例#2
0
        private async Task <string> SetUnverifyAsync(SocketUser socketUser, string time, string data, SocketGuild guild, SocketUser fromUser, bool selfUnverify,
                                                     List <string> toKeep, SocketRole mutedRole)
        {
            var user = await guild.GetUserFromGuildAsync(socketUser.Id);

            await Checker.ValidateAsync(user, guild, selfUnverify);

            var profile = await UnverifyProfileGenerator.CreateProfileAsync(user, guild, time, data, selfUnverify, toKeep, mutedRole);

            UnverifyLog unverifyLogEntity;

            if (selfUnverify)
            {
                unverifyLogEntity = await UnverifyLogger.LogSelfUnverifyAsync(profile, guild);
            }
            else
            {
                unverifyLogEntity = await UnverifyLogger.LogUnverifyAsync(profile, guild, fromUser);
            }

            try
            {
                if (mutedRole != null)
                {
                    await user.SetRoleAsync(mutedRole);
                }

                await user.RemoveRolesAsync(profile.RolesToRemove);

                foreach (var channelOverride in profile.ChannelsToRemove)
                {
                    var channel = guild.GetChannel(channelOverride.ChannelID);
                    await channel?.AddPermissionOverwriteAsync(user, new OverwritePermissions(viewChannel : PermValue.Deny));
                }

                var userEntity = await GrillBotRepository.UsersRepository.GetOrCreateUserAsync(guild.Id, user.Id, UsersIncludes.Unverify);

                userEntity.Unverify = new Database.Entity.Unverify.Unverify()
                {
                    DeserializedChannels = profile.ChannelsToRemove.ConvertAll(o => new ChannelOverride(o.ChannelID, o.Perms)),
                    DeserializedRoles    = profile.RolesToRemove.ConvertAll(o => o.Id),
                    EndDateTime          = profile.EndDateTime,
                    Reason          = profile.Reason,
                    StartDateTime   = profile.StartDateTime,
                    SetLogOperation = unverifyLogEntity
                };

                await GrillBotRepository.CommitAsync();

                Queue.Add(new UnverifyBackgroundTask(guild.Id, user.Id, profile.EndDateTime));

                var pmMessage = MessageGenerator.CreateUnverifyPMMessage(profile, guild);
                await user.SendPrivateMessageAsync(pmMessage);

                return(MessageGenerator.CreateUnverifyMessageToChannel(profile));
            }
            catch (Exception ex)
            {
                if (mutedRole != null)
                {
                    await user.RemoveRoleAsync(mutedRole);
                }

                await user.AddRolesAsync(profile.RolesToRemove);

                foreach (var channelOverride in profile.ChannelsToRemove)
                {
                    var channel = guild.GetChannel(channelOverride.ChannelID);

                    if (channel != null)
                    {
                        await channel.AddPermissionOverwriteAsync(user, channelOverride.Perms);
                    }
                }

                var errorMessage = new LogMessage(LogSeverity.Warning, nameof(UnverifyService), "An error occured when unverify removing access.", ex);
                await Logger.OnLogAsync(errorMessage);

                return(MessageGenerator.CreateUnverifyFailedToChannel(user));
            }
        }