Beispiel #1
0
    private async Task SetPermissions(VerifyCharacterResult result)
    {
        if (Context?.User is not IGuildUser user)
        {
            return;
        }

        var role = Context.Guild?.Roles.FirstOrDefault(x => x.Id == result.VerifiedRoleId);

        if (role is null)
        {
            await ReplyAsync($"However, I could not find the server's verified role. I will notify the server's administrator.");
            await NotifyAdmin($"I successfully verified {user.Mention} as {result.Name} but I could not find the verified role. If you changed this role, please use the `set` command again, e.g. `{await GetPrefix()}set verify VerifiedRoleName FreeCompanyName FFXIVServer` in your server.");

            return;
        }

        try
        {
            await user.AddRoleAsync(role);
            await ReplyAsync($"I have given you the **{role.Name}** role. This includes any exclusive permissions, like channel access, so take a look!");
        }
        catch (HttpException ex) when(ex.HttpCode == HttpStatusCode.Forbidden)
        {
            await ReplyAsync("Unfortunately, I do not have the proper permissions to set your verified role. I will notify the server's administrator.");
            await NotifyAdmin($"I successfully verified {user.Mention} as {result.Name} but I do not have the permissions to set the verified role. Please double check your server permissions and I am able to successfully assign the role.");
        }
    }
Beispiel #2
0
    private async Task SetUserName(VerifyCharacterResult result)
    {
        if (Context?.User is not IGuildUser user)
        {
            return;
        }

        try
        {
            await user.ModifyAsync(properties =>
            {
                properties.Nickname = result.Name;
            });
            await ReplyAsync($"Your nickname in this server has been changed to **{result.Name}**.");
        }
        catch (HttpException ex) when(ex.HttpCode == HttpStatusCode.Forbidden)
        {
            await ReplyAsync("It appears that I do not have the proper permissions to set your nickname. I will notify the server's administrator.");
            await NotifyAdmin($"I successfully verified {user.Mention} as {result.Name} but I do not have the permissions to change his/her nickname. Please double check your server permissions and I am able to successfully change users' nicknames.");
        }
    }
    public async Task <VerifyCharacterResult> Process(VerifyCharacterCriteria criteria)
    {
        _verifyCharacterValidator.ValidateAndThrow(criteria);

        // Get the guild options for free company definition.
        _logger.LogTrace("Getting guild options for guild {Id}.", criteria.GuildId);

        var guildOptionsQuery = new GetOptionsQuery()
        {
            GuildId = criteria.GuildId
        };

        var guildOptions = await _guildAccessor.GetOptions(guildOptionsQuery);

        if (guildOptions?.FreeCompany is null || guildOptions.VerifiedRoleId == 0)
        {
            _logger.LogDebug("Free Company options not defined for guild {Id}.", criteria.GuildId);

            return(new VerifyCharacterResult()
            {
                Status = Status.FreeCompanyUndefined
            });
        }

        var result = new VerifyCharacterResult()
        {
            FreeCompanyName = guildOptions.FreeCompany.Name,
            VerifiedRoleId  = guildOptions.VerifiedRoleId
        };

        // Parse the query into name/server.
        _logger.LogTrace("Parsing query: {Query}.", criteria.Query);

        var(name, _) = NameServerEngine.Parse(criteria.Query);

        // Search for the character.
        _logger.LogTrace("Searching for {CharacterName} on {ServerName}.", name, guildOptions.FreeCompany.Server);

        var searchQuery = new SearchCharacterQuery()
        {
            Name   = name,
            Server = guildOptions.FreeCompany.Server
        };

        var searchData = await _xivApiAccessor.SearchCharacter(searchQuery);

        var characterId = searchData.Results?.FirstOrDefault()?.Id;

        _logger.LogDebug("Got character Id {Id}.", characterId);

        if (characterId is null)
        {
            result.Status = Status.NotVerified;
            result.Name   = searchData.Results?.FirstOrDefault()?.Name;
            return(result);
        }

        // Check if character is already attached to a user.
        _logger.LogTrace("Checking database if {CharacterName} has already been tied to a user. CharacterId: {CharacterId}", name, characterId);

        var checkQuery = new SearchUserQuery()
        {
            CharacterId = characterId.Value
        };

        var user = await _userAccessor.SearchUser(checkQuery);

        if (user is object)
        {
            _logger.LogDebug("{CharacterName} ({CharacterId}) has already been tied to UserId {UserId}.", name, characterId, user.Id);
            result.Status         = Status.CharacterAlreadyVerified;
            result.Name           = searchData.Results?.FirstOrDefault()?.Name;
            result.VerifiedUserId = user.Id;
            return(result);
        }

        // Get the character.
        _logger.LogTrace("Getting character with Id {Id}.", characterId);

        var getQuery = new GetCharacterQuery()
        {
            Id = characterId.Value
        };

        var getData = await _xivApiAccessor.GetCharacter(getQuery);

        var characterFcId = getData?.Character?.FreeCompanyId;

        _logger.LogDebug("Got character Free Company Id {FcId}.", characterFcId);

        result.Name = getData?.Character?.Name;

        if (characterFcId != guildOptions.FreeCompany.Id)
        {
            result.Status = Status.NotVerified;
            _logger.LogDebug("{Name} failed verification. Character FC: {CFcId}. Guild FC: {FcId}", result.Name, characterFcId, guildOptions.FreeCompany.Id);
            return(result);
        }

        result.Status         = Status.Verified;
        result.VerifiedUserId = criteria.UserId;
        _logger.LogDebug("{Name} has been verified with Free Company Id {FcId}.", result.Name, guildOptions.FreeCompany.Id);

        // Save character-user map to database.
        _logger.LogDebug("Saving {Name} to database with User Id {UserId}.", result.Name, criteria.UserId);

        var dataUser = await _userAccessor.GetUser(criteria.UserId) ?? new()
        {
            Id = criteria.UserId
        };
        var mergedUser = dataUser.Merge(characterId.Value);

        mergedUser.Name = criteria.Name;
        mergedUser.Nicknames[criteria.GuildId] = result.Name ?? "";

        await _userAccessor.SaveUser(mergedUser);

        return(result);
    }
}