sync method
Syncs command builders with discord immediately. Warning: Client could not be ready at the function execution. Use syncOnReady for proper behavior
Implementation
Future<void> sync({ICommandsSync syncRule = const ManualCommandSync()}) async {
if (!await syncRule.shouldSync(this._commandBuilders)) {
return;
}
final commandPartition = _partition<SlashCommandBuilder>(this._commandBuilders, (element) => element.guild == null);
final globalCommands = commandPartition.first;
final groupedGuildCommands = _groupSlashCommandBuilders(commandPartition.last);
final globalCommandsResponse = await this.interactionsEndpoints
.bulkOverrideGlobalCommands(this._client.app.id, globalCommands)
.toList();
_extractCommandIds(globalCommandsResponse);
this._registerCommandHandlers(globalCommandsResponse, globalCommands);
for(final entry in groupedGuildCommands.entries) {
final response = await this.interactionsEndpoints
.bulkOverrideGuildCommands(this._client.app.id, entry.key, entry.value)
.toList();
_extractCommandIds(response);
this._registerCommandHandlers(response, entry.value);
await this.interactionsEndpoints.bulkOverrideGuildCommandsPermissions(this._client.app.id, entry.key, entry.value);
}
this._commandBuilders.clear(); // Cleanup after registering command since we don't need this anymore
this._logger.info("Finished bulk overriding slash commands and permissions");
if (this._commands.isNotEmpty) {
this.onSlashCommand.listen((event) async {
final commandHash = _determineInteractionCommandHandler(event.interaction);
this._logger.info("Executing command with hash [$commandHash]");
if (this._commandHandlers.containsKey(commandHash)) {
await this._commandHandlers[commandHash]!(event);
}
});
this._logger.info("Finished registering ${this._commandHandlers.length} commands!");
}
if (this._buttonHandlers.isNotEmpty) {
this.onButtonEvent.listen((event) {
if (this._buttonHandlers.containsKey(event.interaction.customId)) {
this._logger.info("Executing button with id [${event.interaction.customId}]");
this._buttonHandlers[event.interaction.customId]!(event);
} else {
this._logger.warning("Received event for unknown button: ${event.interaction.customId}");
}
});
}
if (this._multiselectHandlers.isNotEmpty) {
this.onMultiselectEvent.listen((event) {
if (this._multiselectHandlers.containsKey(event.interaction.customId)) {
this._logger.info("Executing multiselect with id [${event.interaction.customId}]");
this._multiselectHandlers[event.interaction.customId]!(event);
} else {
this._logger.warning("Received event for unknown dropdown: ${event.interaction.customId}");
}
});
}
if (this._autocompleteHandlers.isNotEmpty) {
this.onAutocompleteEvent.listen((event) {
final name = event.focusedOption.name;
if (this._autocompleteHandlers.containsKey(name)) {
this._logger.info("Executing autocomplete with id [$name]");
this._autocompleteHandlers[name]!(event);
} else {
this._logger.warning("Received event for unknown dropdown: $name");
}
});
}
}