/// <summary> /// Creates a new character role from the given Discord role and access condition. /// </summary> /// <param name="guildID">The ID of the guild the role is on.</param> /// <param name="roleID">The ID of the discord role.</param> /// <param name="access">The access conditions.</param> /// <param name="ct">The cancellation token in use.</param> /// <returns>A creation result which may or may not have succeeded.</returns> public async Task <Result <CharacterRole> > CreateCharacterRoleAsync ( Snowflake guildID, Snowflake roleID, RoleAccess access, CancellationToken ct = default ) { var getExistingRoleResult = await GetCharacterRoleAsync(guildID, roleID, ct); if (getExistingRoleResult.IsSuccess) { return(new UserError ( "That role is already registered as a character role." )); } var getServerResult = await _servers.GetOrRegisterServerAsync(guildID, ct); if (!getServerResult.IsSuccess) { return(Result <CharacterRole> .FromError(getServerResult)); } var server = getServerResult.Entity; var characterRole = _database.CreateProxy <CharacterRole>(server, roleID, access); _database.CharacterRoles.Update(characterRole); await _database.SaveChangesAsync(ct); return(characterRole); }
/// <summary> /// Creates a new character role from the given Discord role and access condition. /// </summary> /// <param name="role">The discord role.</param> /// <param name="access">The access conditions.</param> /// <param name="ct">The cancellation token in use.</param> /// <returns>A creation result which may or may not have succeeded.</returns> public async Task <CreateEntityResult <CharacterRole> > CreateCharacterRoleAsync ( IRole role, RoleAccess access, CancellationToken ct = default ) { var getExistingRoleResult = await GetCharacterRoleAsync(role, ct); if (getExistingRoleResult.IsSuccess) { return(CreateEntityResult <CharacterRole> .FromError ( "That role is already registered as a character role." )); } var getServerResult = await _servers.GetOrRegisterServerAsync(role.Guild, ct); if (!getServerResult.IsSuccess) { return(CreateEntityResult <CharacterRole> .FromError(getServerResult)); } var server = getServerResult.Entity; var characterRole = _database.CreateProxy <CharacterRole>(server, (long)role.Id, access); _database.CharacterRoles.Update(characterRole); await _database.SaveChangesAsync(ct); return(characterRole); }
/// <inheritdoc /> public async Task <CreateEntityResult <Character> > CreateCharacterAsync ( User user, Server server, string name, string?avatarUrl = null, string?nickname = null, string?summary = null, string?description = null, string?pronounFamily = null, CancellationToken ct = default ) { user = _database.NormalizeReference(user); server = _database.NormalizeReference(server); avatarUrl ??= _content.GetDefaultAvatarUri().ToString(); nickname ??= name; summary ??= "No summary set."; description ??= "No description set."; pronounFamily ??= new TheyPronounProvider().Family; // Use dummy values here - we'll set them with the service so we can ensure they're correctly formatted. var character = _database.CreateProxy <Character> ( user, server, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty ); _database.Characters.Update(character); var modifyEntityResult = await SetCharacterNameAsync(character, name, ct); if (!modifyEntityResult.IsSuccess) { return(CreateEntityResult <Character> .FromError(modifyEntityResult)); } modifyEntityResult = await SetCharacterAvatarAsync(character, avatarUrl, ct); if (!modifyEntityResult.IsSuccess) { return(CreateEntityResult <Character> .FromError(modifyEntityResult)); } modifyEntityResult = await SetCharacterNicknameAsync(character, nickname, ct); if (!modifyEntityResult.IsSuccess) { return(CreateEntityResult <Character> .FromError(modifyEntityResult)); } modifyEntityResult = await SetCharacterSummaryAsync(character, summary, ct); if (!modifyEntityResult.IsSuccess) { return(CreateEntityResult <Character> .FromError(modifyEntityResult)); } modifyEntityResult = await SetCharacterDescriptionAsync(character, description, ct); if (!modifyEntityResult.IsSuccess) { return(CreateEntityResult <Character> .FromError(modifyEntityResult)); } modifyEntityResult = await SetCharacterPronounsAsync(character, pronounFamily, ct); if (!modifyEntityResult.IsSuccess) { return(CreateEntityResult <Character> .FromError(modifyEntityResult)); } await _database.SaveChangesAsync(ct); return(character); }