Skip to content

Commit

Permalink
Remove the need to specify the application ID when creating a client.
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Aug 21, 2023
1 parent fd56dbc commit c6284ae
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 70 deletions.
23 changes: 1 addition & 22 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:nyxx/src/http/managers/gateway_manager.dart';
import 'package:nyxx/src/intents.dart';
import 'package:nyxx/src/manager_mixin.dart';
import 'package:nyxx/src/api_options.dart';
import 'package:nyxx/src/models/application.dart';
import 'package:nyxx/src/models/guild/guild.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/plugin/plugin.dart';
Expand Down Expand Up @@ -53,15 +52,7 @@ abstract class Nyxx {
..fine('Token: ${apiOptions.token}, Authorization: ${apiOptions.authorizationHeader}, User-Agent: ${apiOptions.userAgent}')
..fine('Plugins: ${clientOptions.plugins.map((plugin) => plugin.name).join(', ')}');

return _doConnect(apiOptions, clientOptions, () async {
final client = NyxxRest._(apiOptions, clientOptions);

if (clientOptions.applicationId != null) {
return client..application = client.applications[clientOptions.applicationId!];
}

return client..application = await client.applications.fetchCurrentApplication();
}, clientOptions.plugins);
return _doConnect(apiOptions, clientOptions, () async => NyxxRest._(apiOptions, clientOptions), clientOptions.plugins);
}

/// Create an instance of [NyxxGateway] that can perform requests to the HTTP API, connects
Expand All @@ -86,12 +77,6 @@ abstract class Nyxx {
return _doConnect(apiOptions, clientOptions, () async {
final client = NyxxGateway._(apiOptions, clientOptions);

if (clientOptions.applicationId != null) {
client.application = client.applications[clientOptions.applicationId!];
} else {
client.application = await client.applications.fetchCurrentApplication();
}

// We can't use client.gateway as it is not initialized yet
final gatewayManager = GatewayManager(client);

Expand All @@ -117,9 +102,6 @@ class NyxxRest with ManagerMixin implements Nyxx {
@override
late final HttpHandler httpHandler = HttpHandler(this);

/// The application associated with this client.
late final PartialApplication application;

@override
Logger get logger => options.logger;

Expand Down Expand Up @@ -161,9 +143,6 @@ class NyxxGateway with ManagerMixin, EventMixin implements NyxxRest {
@override
late final HttpHandler httpHandler = HttpHandler(this);

@override
late final PartialApplication application;

/// The [Gateway] used by this client to send and receive Gateway events.
// Initialized in connectGateway due to a circular dependency
@override
Expand Down
6 changes: 0 additions & 6 deletions lib/src/client_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:nyxx/src/models/guild/member.dart';
import 'package:nyxx/src/models/guild/scheduled_event.dart';
import 'package:nyxx/src/models/message/message.dart';
import 'package:nyxx/src/models/role.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/models/sticker/global_sticker.dart';
import 'package:nyxx/src/models/sticker/guild_sticker.dart';
import 'package:nyxx/src/models/user/user.dart';
Expand Down Expand Up @@ -93,9 +92,6 @@ class RestClientOptions extends ClientOptions {
/// The [CacheConfig] to use for the [GuildApplicationCommandManager.permissionsCache] cache.
final CacheConfig<CommandPermissions> commandPermissionsConfig;

/// The ID of the application the client is authenticating for.
final Snowflake? applicationId;

/// Create a new [RestClientOptions].
const RestClientOptions({
super.plugins,
Expand All @@ -118,7 +114,6 @@ class RestClientOptions extends ClientOptions {
this.globalStickerCacheConfig = const CacheConfig(),
this.applicationCommandConfig = const CacheConfig(),
this.commandPermissionsConfig = const CacheConfig(),
this.applicationId,
});
}

Expand Down Expand Up @@ -150,6 +145,5 @@ class GatewayClientOptions extends RestClientOptions {
super.voiceStateConfig,
super.applicationCommandConfig,
super.commandPermissionsConfig,
super.applicationId,
});
}
23 changes: 11 additions & 12 deletions lib/src/http/managers/application_command_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {
Snowflake? get _guildId;

/// The ID of the application this manager is for.
final Snowflake applicationId;
late final Future<Snowflake> _applicationId = client.applications.fetchCurrentApplication().then((application) => application.id);

/// Create a new [ApplicationCommandManager].
ApplicationCommandManager(super.config, super.client, {required this.applicationId, required super.identifier});
ApplicationCommandManager(super.config, super.client, {required super.identifier});

@override
PartialApplicationCommand operator [](Snowflake id) => PartialApplicationCommand(id: id, manager: this);
Expand Down Expand Up @@ -105,7 +105,7 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {

/// List the commands belonging to the application.
Future<List<ApplicationCommand>> list({bool? withLocalizations}) async {
final route = HttpRoute()..applications(id: applicationId.toString());
final route = HttpRoute()..applications(id: (await _applicationId).toString());
if (_guildId != null) route.guilds(id: _guildId!.toString());
route.commands();

Expand All @@ -120,7 +120,7 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {

@override
Future<ApplicationCommand> fetch(Snowflake id) async {
final route = HttpRoute()..applications(id: applicationId.toString());
final route = HttpRoute()..applications(id: (await _applicationId).toString());
if (_guildId != null) route.guilds(id: _guildId!.toString());
route.commands(id: id.toString());

Expand All @@ -135,7 +135,7 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {

@override
Future<ApplicationCommand> create(ApplicationCommandBuilder builder) async {
final route = HttpRoute()..applications(id: applicationId.toString());
final route = HttpRoute()..applications(id: (await _applicationId).toString());
if (_guildId != null) route.guilds(id: _guildId!.toString());
route.commands();

Expand All @@ -150,7 +150,7 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {

@override
Future<ApplicationCommand> update(Snowflake id, ApplicationCommandUpdateBuilder builder) async {
final route = HttpRoute()..applications(id: applicationId.toString());
final route = HttpRoute()..applications(id: (await _applicationId).toString());
if (_guildId != null) route.guilds(id: _guildId!.toString());
route.commands(id: id.toString());

Expand All @@ -165,7 +165,7 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {

@override
Future<void> delete(Snowflake id) async {
final route = HttpRoute()..applications(id: applicationId.toString());
final route = HttpRoute()..applications(id: (await _applicationId).toString());
if (_guildId != null) route.guilds(id: _guildId!.toString());
route.commands(id: id.toString());

Expand All @@ -177,7 +177,7 @@ abstract class ApplicationCommandManager extends Manager<ApplicationCommand> {

/// Remove all existing commands and replace them with the commands defined in [builders].
Future<List<ApplicationCommand>> bulkOverride(List<ApplicationCommandBuilder> builders) async {
final route = HttpRoute()..applications(id: applicationId.toString());
final route = HttpRoute()..applications(id: (await _applicationId).toString());
if (_guildId != null) route.guilds(id: _guildId!.toString());
route.commands();

Expand Down Expand Up @@ -208,7 +208,6 @@ class GuildApplicationCommandManager extends ApplicationCommandManager {
GuildApplicationCommandManager(
super.config,
super.client, {
required super.applicationId,
required this.guildId,
required CacheConfig<CommandPermissions> permissionsConfig,
}) : permissionsCache = Cache(client, '$guildId.commandPermissions', permissionsConfig),
Expand Down Expand Up @@ -237,7 +236,7 @@ class GuildApplicationCommandManager extends ApplicationCommandManager {
/// List all the [CommandPermissions] in this guild.
Future<List<CommandPermissions>> listPermissions() async {
final route = HttpRoute()
..applications(id: applicationId.toString())
..applications(id: (await _applicationId).toString())
..guilds(id: guildId.toString())
..commands()
..permissions();
Expand All @@ -253,7 +252,7 @@ class GuildApplicationCommandManager extends ApplicationCommandManager {
/// Fetch the permissions for a command.
Future<CommandPermissions> fetchPermissions(Snowflake id) async {
final route = HttpRoute()
..applications(id: applicationId.toString())
..applications(id: (await _applicationId).toString())
..guilds(id: guildId.toString())
..commands(id: id.toString())
..permissions();
Expand All @@ -275,5 +274,5 @@ class GlobalApplicationCommandManager extends ApplicationCommandManager {
Null get _guildId => null;

/// Create a new [GlobalApplicationCommandManager].
GlobalApplicationCommandManager(super.config, super.client, {required super.applicationId}) : super(identifier: 'commands');
GlobalApplicationCommandManager(super.config, super.client) : super(identifier: 'commands');
}
27 changes: 13 additions & 14 deletions lib/src/http/managers/interaction_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ class InteractionManager {
/// The client for this [InteractionManager].
final NyxxRest client;

/// The ID of the application for this [InteractionManager].
final Snowflake applicationId;

/// Create a new [InteractionManager].
InteractionManager(this.client, {required this.applicationId});
InteractionManager(this.client);

Interaction<dynamic> parse(Map<String, Object?> raw) {
final type = InteractionType.parse(raw['type'] as int);
Expand Down Expand Up @@ -266,16 +263,17 @@ class InteractionManager {
}

/// Fetch an interaction's original response.
Future<Message> fetchOriginalResponse(String token) => _fetchResponse(token, '@original');
Future<Message> fetchOriginalResponse(Snowflake applicationId, String token) => _fetchResponse(applicationId, token, '@original');

/// Update an interaction's original response.
Future<Message> updateOriginalResponse(String token, MessageUpdateBuilder builder) => _updateResponse(token, '@original', builder);
Future<Message> updateOriginalResponse(Snowflake applicationId, String token, MessageUpdateBuilder builder) =>
_updateResponse(applicationId, token, '@original', builder);

/// Delete an interaction's original response.
Future<void> deleteOriginalResponse(String token) => _deleteResponse(token, '@original');
Future<void> deleteOriginalResponse(Snowflake applicationId, String token) => _deleteResponse(applicationId, token, '@original');

/// Create a followup to an interaction.
Future<Message> createFollowup(String token, MessageBuilder builder) async {
Future<Message> createFollowup(Snowflake applicationId, String token, MessageBuilder builder) async {
final route = HttpRoute()
..webhooks(id: applicationId.toString())
..add(HttpRoutePart(token));
Expand Down Expand Up @@ -319,15 +317,16 @@ class InteractionManager {
}

/// Fetch a followup to an interaction.
Future<Message> fetchFollowup(String token, Snowflake messageId) => _fetchResponse(token, messageId.toString());
Future<Message> fetchFollowup(Snowflake applicationId, String token, Snowflake messageId) => _fetchResponse(applicationId, token, messageId.toString());

/// Update a followup to an interaction.
Future<Message> updateFollowup(String token, Snowflake messageId, MessageUpdateBuilder builder) => _updateResponse(token, messageId.toString(), builder);
Future<Message> updateFollowup(Snowflake applicationId, String token, Snowflake messageId, MessageUpdateBuilder builder) =>
_updateResponse(applicationId, token, messageId.toString(), builder);

/// Delete a followup to an interaction.
Future<void> deleteFollowup(String token, Snowflake messageId) => _deleteResponse(token, messageId.toString());
Future<void> deleteFollowup(Snowflake applicationId, String token, Snowflake messageId) => _deleteResponse(applicationId, token, messageId.toString());

Future<Message> _fetchResponse(String token, String messageId) async {
Future<Message> _fetchResponse(Snowflake applicationId, String token, String messageId) async {
final route = HttpRoute()
..webhooks(id: applicationId.toString(), token: token)
..messages(id: messageId);
Expand All @@ -339,7 +338,7 @@ class InteractionManager {
return (client.channels[channelId] as PartialTextChannel).messages.parse(response.jsonBody as Map<String, Object?>);
}

Future<Message> _updateResponse(String token, String messageId, MessageUpdateBuilder builder) async {
Future<Message> _updateResponse(Snowflake applicationId, String token, String messageId, MessageUpdateBuilder builder) async {
final route = HttpRoute()
..webhooks(id: applicationId.toString(), token: token)
..messages(id: messageId.toString());
Expand Down Expand Up @@ -381,7 +380,7 @@ class InteractionManager {
return (client.channels[channelId] as PartialTextChannel).messages.parse(response.jsonBody as Map<String, Object?>);
}

Future<void> _deleteResponse(String token, String messageId) async {
Future<void> _deleteResponse(Snowflake applicationId, String token, String messageId) async {
final route = HttpRoute()
..webhooks(id: applicationId.toString(), token: token)
..messages(id: messageId);
Expand Down
6 changes: 3 additions & 3 deletions lib/src/manager_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ mixin ManagerMixin implements Nyxx {
GlobalStickerManager get stickers => GlobalStickerManager(options.globalStickerCacheConfig, this as NyxxRest);

/// A [GlobalApplicationCommandManager] that manages global application commands.
GlobalApplicationCommandManager get commands =>
GlobalApplicationCommandManager(options.applicationCommandConfig, this as NyxxRest, applicationId: (this as NyxxRest).application.id);
GlobalApplicationCommandManager get commands => GlobalApplicationCommandManager(options.applicationCommandConfig, this as NyxxRest);

InteractionManager get interactions => InteractionManager(this as NyxxRest, applicationId: (this as NyxxRest).application.id);
/// An [InteractionManager] that manages interactions for this client.
InteractionManager get interactions => InteractionManager(this as NyxxRest);
}
18 changes: 9 additions & 9 deletions lib/src/models/interaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,30 @@ mixin MessageResponse<T> on Interaction<T> {
} else {
assert(builder.isEphemeral == _wasEphemeral || builder.isEphemeral == null, 'Cannot change the value of isEphemeral between acknowledge and respond');

await manager.updateOriginalResponse(token, builder);
await manager.updateOriginalResponse(applicationId, token, builder);
}
}

/// Fetch the original response to this interaction.
Future<Message> fetchOriginalResponse() => manager.fetchOriginalResponse(token);
Future<Message> fetchOriginalResponse() => manager.fetchOriginalResponse(applicationId, token);

/// Update the original response to this interaction.
Future<Message> updateOriginalResponse(MessageUpdateBuilder builder) => manager.updateOriginalResponse(token, builder);
Future<Message> updateOriginalResponse(MessageUpdateBuilder builder) => manager.updateOriginalResponse(applicationId, token, builder);

/// Delete the original response to this interaction.
Future<void> deleteOriginalResponse() => manager.deleteOriginalResponse(token);
Future<void> deleteOriginalResponse() => manager.deleteOriginalResponse(applicationId, token);

/// Create a followup to this interaction.
Future<Message> createFollowup(MessageBuilder builder) => manager.createFollowup(token, builder);
Future<Message> createFollowup(MessageBuilder builder) => manager.createFollowup(applicationId, token, builder);

/// Fetch a followup to this interaction.
Future<Message> fetchFollowup(Snowflake id) => manager.fetchFollowup(token, id);
Future<Message> fetchFollowup(Snowflake id) => manager.fetchFollowup(applicationId, token, id);

/// Update a followup to this interaction.
Future<Message> updateFollowup(Snowflake id, MessageUpdateBuilder builder) => manager.updateFollowup(token, id, builder);
Future<Message> updateFollowup(Snowflake id, MessageUpdateBuilder builder) => manager.updateFollowup(applicationId, token, id, builder);

/// Delete a followup to this interaction.
Future<void> deleteFollowup(Snowflake id) => manager.deleteFollowup(token, id);
Future<void> deleteFollowup(Snowflake id) => manager.deleteFollowup(applicationId, token, id);
}

mixin ModalResponse<T> on Interaction<T> {
Expand Down Expand Up @@ -281,7 +281,7 @@ class MessageComponentInteraction extends Interaction<MessageComponentInteractio
assert(updateMessage == _didUpdateMessage || updateMessage == null, 'Cannot change the value of updateMessage between acknowledge and respond');
assert(builder.isEphemeral == _wasEphemeral || builder.isEphemeral == null, 'Cannot change the value of isEphemeral between acknowledge and respond');

await manager.updateOriginalResponse(token, builder);
await manager.updateOriginalResponse(applicationId, token, builder);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/rest_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main() {
// refer to the same variable if we use setUp and tearDown. use the All variants
// to mitigate this.
setUpAll(() async {
client = await Nyxx.connectRest(testToken!, options: RestClientOptions(applicationId: Snowflake.zero));
client = await Nyxx.connectRest(testToken!);
});

tearDownAll(() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void checkCommandPermissions(CommandPermissions permissions) {
void main() {
testManager<ApplicationCommand, GlobalApplicationCommandManager>(
'GlobalApplicationCommandManager',
(config, client) => GlobalApplicationCommandManager(config, client, applicationId: Snowflake.zero),
(config, client) => GlobalApplicationCommandManager(config, client),
RegExp(r'/applications/0/commands/\d+'),
'/applications/0/commands',
sampleObject: sampleCommand,
Expand Down Expand Up @@ -101,7 +101,6 @@ void main() {
(config, client) => GuildApplicationCommandManager(
config,
client,
applicationId: Snowflake.zero,
guildId: Snowflake(1),
permissionsConfig: const CacheConfig(),
),
Expand Down
2 changes: 1 addition & 1 deletion test/unit/http/managers/interaction_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void main() {
source: sampleCommandInteraction,
parse: (manager) => manager.parse,
check: checkCommandInteraction,
).runWithManager(InteractionManager(client, applicationId: Snowflake.zero));
).runWithManager(InteractionManager(client));
});

// Endpoints are tested in webhook_manager_test.dart as the implementation is the same.
Expand Down

0 comments on commit c6284ae

Please sign in to comment.