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

Slash commands #145

Merged
merged 41 commits into from
Jan 5, 2021
Merged

Slash commands #145

merged 41 commits into from
Jan 5, 2021

Conversation

HopeBaron
Copy link
Member

@HopeBaron HopeBaron commented Dec 18, 2020

What this PR should Implement

  • Gateways related to slash commands.
  • REST endpoints
  • Core representations

Issues encountered

Slash Commands and Interactions bring something entirely new to the table: the ability to interact with an application without needing a bot user in the guild. As you read through this documentation, you'll see that bot tokens are only referenced as a helpful alternative to doing a client credentials auth flow. Slash Commands do not depend on a bot user in the guild; responding to interactions does not require a bot token.

As the Discord docs states , neither a slash command doesn't depend on a bot user, nor a bot user requires a slash commands to work.

Solutions proposed

One simple solution to the problem, is to allow applicationId be nullable as follows:

    suspend fun createGlobalApplicationCommand(
        name: String,
        description: String,
        builder: GlobalApplicationCommandCreateBuilder.() -> Unit
    ) {
        if(resources.applicationId == null) throw IllegalArgumentException("applicationId must not be null")
        val request = GlobalApplicationCommandCreateBuilder(name, description).apply(builder).toRequest()
        rest.interaction.createGlobalApplicationCommand(applicationId, request)
    }

But this wouldn't provide the user with a useful message or warning if the applicationId was null. In addition, this would require to validate applicationId each time we work with it.


Provide an annotation warning the same way we did for intents.
However annotations can be ignored and do not provide a strict message as well.


Move interactions into their own class that works along side Kord e.g: SlashCommands

This is the best solution we have at the moment.
The problem is that this solution limts Kord's control over slash commands overall.

val commands: SlashCommands = Kord.toSlashCommand(appId: String)

if we were going to use this object to create a command from message, or any other context we will need to keep a reference and limits the flexibility of code writing.

@HopeBaron HopeBaron requested a review from BartArys December 18, 2020 18:35
@HopeBaron
Copy link
Member Author

HopeBaron commented Dec 19, 2020

The team agreed on the following solution:

  • A rest-only entity which is responsible for application commands created (currently named SlashCommands)
    This would allow creating, editing, or deleting slash command's related records without a gateway.

  • Kord will allow a nullable appId

  • Two versions of Interaction events will be created.

Interaction - offers data from the INTERACTION_CREATE event with no functionality (Kord will use this if appId is null)
InteractionBehavior - offers data, with functionality, and derives from Interaction

How does this solve the problem

Kord will introduce a event handler for the interaction create event, which keeps track of the nullity of the appId. In-case the appId was null, the event handler won't emit an InteractionBehavior object. A filtered flow for that event will always be empty. Otherwise, the user will be able to pick both events from the event flow.

Below are some code samples

val kord = Kord(bot_id) { appid = null }
kord.on<InteractionBehavior> {  // flow will always be empty since `appId` is null

}
val kord = Kord(bot_id) { appid = null }
kord.on<Interaction> {  // flow emits even if appId is null

}
val kord = Kord(bot_id) { appid = appId }
kord.on<InteractionBehavior> {  // flow emits since appId is not null

}

Copy link
Contributor

@BartArys BartArys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for the most part, missed quite a few @KordPreview in the gateway module though.

@HopeBaron HopeBaron requested a review from BartArys January 3, 2021 06:15
@HopeBaron HopeBaron changed the title [WIP] - Slash commands Slash commands Jan 3, 2021
@HopeBaron HopeBaron requested a review from BartArys January 3, 2021 15:36
# Conflicts:
#	core/src/main/kotlin/Kord.kt
#	core/src/main/kotlin/behavior/GuildBehavior.kt
#	core/src/main/kotlin/cache/data/MemberData.kt
#	rest/src/main/kotlin/route/Route.kt
#	rest/src/main/kotlin/service/RestClient.kt
@HopeBaron HopeBaron merged commit df707f1 into 0.7.x Jan 5, 2021
@HopeBaron HopeBaron deleted the slash-commands branch March 11, 2021 07:33
@HopeBaron HopeBaron mentioned this pull request Apr 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants