Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use guild role fetch endpoint #682

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/src/http/managers/role_manager.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:nyxx/src/builders/role.dart';
import 'package:nyxx/src/errors.dart';
import 'package:nyxx/src/http/managers/manager.dart';
import 'package:nyxx/src/http/request.dart';
import 'package:nyxx/src/http/route.dart';
Expand Down Expand Up @@ -69,14 +68,16 @@ class RoleManager extends Manager<Role> {

@override
Future<Role> fetch(Snowflake id) async {
// There isn't an endpoint to fetch a single role. Re-fetch all the roles to ensure they are up to date,
// and return the matching role.
final roles = await list();
final route = HttpRoute()
..guilds(id: guildId.toString())
..roles(id: id.toString());

return roles.firstWhere(
(role) => role.id == id,
orElse: () => throw RoleNotFoundException(guildId, id),
);
final request = BasicRequest(route);
final response = await client.httpHandler.executeSafe(request);
final role = parse(response.jsonBody as Map<String, Object?>);

client.updateCacheWith(role);
return role;
}

@override
Expand Down
2 changes: 0 additions & 2 deletions test/unit/http/managers/role_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ void main() {
RegExp(r'/guilds/0/roles(/\d+)?'),
'/guilds/0/roles',
sampleObject: sampleRole,
// Fetch implementation internally uses `list()`, so we return a list
fetchObjectOverride: [sampleRole],
sampleMatches: checkRole,
additionalParsingTests: [],
additionalEndpointTests: [
Expand Down
Loading