Skip to content

Commit

Permalink
use guild role fetch endpoint (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexedia authored Aug 13, 2024
1 parent cac408e commit 71b4424
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
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

0 comments on commit 71b4424

Please sign in to comment.