/// <summary> /// Deletes the given character. /// </summary> /// <param name="guildUser">The user that owns the character.</param> /// <param name="character">The character to delete.</param> /// <returns>A deletion result which may or may not have succeeded.</returns> public async Task <DeleteEntityResult> DeleteCharacterAsync(IGuildUser guildUser, Character character) { var getCurrentCharacter = await _characters.GetCurrentCharacterAsync(character.Owner, character.Server); if (getCurrentCharacter.IsSuccess) { // Forcibly load the role so we can access it later _ = getCurrentCharacter.Entity.Role; } var deleteCharacter = await _characters.DeleteCharacterAsync(character); if (!deleteCharacter.IsSuccess) { return(deleteCharacter); } if (!getCurrentCharacter.IsSuccess) { return(DeleteEntityResult.FromSuccess()); } var currentCharacter = getCurrentCharacter.Entity; if (currentCharacter != character) { return(DeleteEntityResult.FromSuccess()); } // Update the user's nickname var updateNickname = await UpdateUserNickname(guildUser); if (!updateNickname.IsSuccess) { return(DeleteEntityResult.FromError(updateNickname)); } var updateRoles = await _characterRoles.UpdateUserRolesAsync(guildUser, getCurrentCharacter.Entity); if (!updateRoles.IsSuccess) { return(DeleteEntityResult.FromError(updateRoles)); } return(DeleteEntityResult.FromSuccess()); }
/// <summary> /// Updates the roles of the given user, removing old roles and applying new ones. /// </summary> /// <param name="guildUser">The user.</param> /// <param name="previousCharacter">The character they previously were.</param> /// <returns>A modification result which may or may not have succeeded.</returns> public async Task <ModifyEntityResult> UpdateUserRolesAsync ( IGuildUser guildUser, Character?previousCharacter = null ) { var getUser = await _users.GetOrRegisterUserAsync(guildUser); if (!getUser.IsSuccess) { return(ModifyEntityResult.FromError(getUser)); } var getServer = await _servers.GetOrRegisterServerAsync(guildUser.Guild); if (!getServer.IsSuccess) { return(ModifyEntityResult.FromError(getServer)); } var user = getUser.Entity; var server = getServer.Entity; var getNewCharacter = await _characters.GetCurrentCharacterAsync(user, server); if (!getNewCharacter.IsSuccess) { // Clear any old role if (previousCharacter?.Role is null) { return(ModifyEntityResult.FromSuccess()); } var oldRole = guildUser.Guild.GetRole((ulong)previousCharacter.Role.DiscordID); if (oldRole is null) { return(ModifyEntityResult.FromSuccess()); } var removeRole = await _discord.RemoveUserRoleAsync(guildUser, oldRole); if (!removeRole.IsSuccess) { return(removeRole); } return(ModifyEntityResult.FromSuccess()); } var newCharacter = getNewCharacter.Entity; // First, quick sanity check - do we need to remove the role? if (!(previousCharacter?.Role is null) && newCharacter.Role == previousCharacter.Role) { return(ModifyEntityResult.FromSuccess()); } // Clear any old role if (!(previousCharacter?.Role is null)) { var oldRole = guildUser.Guild.GetRole((ulong)previousCharacter.Role.DiscordID); if (!(oldRole is null)) { var removeRole = await _discord.RemoveUserRoleAsync(guildUser, oldRole); if (!removeRole.IsSuccess) { return(removeRole); } } } if (newCharacter.Role is null) { return(ModifyEntityResult.FromSuccess()); } // Apply any new role var newRole = guildUser.Guild.GetRole((ulong)newCharacter.Role.DiscordID); if (newRole is null) { return(ModifyEntityResult.FromSuccess()); } var addRole = await _discord.AddUserRoleAsync(guildUser, newRole); if (!addRole.IsSuccess) { return(addRole); } return(ModifyEntityResult.FromSuccess()); }