Can you send an ephemeral response from a public slash command?

^
Solution:
```kotlin check { val user = userFor(event)!! val usage = usageService.checkUsage(user.id) event.extraData["usageResponse"] = usage...
Jump to solution
63 Replies
gdude
gdude•6mo ago
the discord API accepts that and it's exposed via respondOpposite, but it probably won't actually be sent as ephemeral what's your use-case?
Shaun
ShaunOP•6mo ago
It's a public slash command, public response is fine, but if there is an error, then I want just the user who sent the command to see it.
gdude
gdude•6mo ago
what kind of error did you have in mind?
Shaun
ShaunOP•6mo ago
No description
gdude
gdude•6mo ago
could you do that check in a check instead?
Shaun
ShaunOP•6mo ago
wdym?
gdude
gdude•6mo ago
using the checks system
gdude
gdude•6mo ago
just write a check that fails with an error message
Shaun
ShaunOP•6mo ago
Okay I'll try that thanks
gdude
gdude•6mo ago
checks don't have access to command arguments if you need that then you'll need to do something else
Shaun
ShaunOP•6mo ago
Ugh that causes problems, because I'd need to hit my database twice
gdude
gdude•6mo ago
that's what the event context is for
Shaun
ShaunOP•6mo ago
Because I output the remaining quota in the message too
gdude
gdude•6mo ago
https://docs.kordex.dev/events.html#custom-context event.extraData.set("key", yourData) event.extraData.getOf<YourDataType>("key") see, I think of things /lh
Shaun
ShaunOP•6mo ago
I can't acess user.id within the check
gdude
gdude•6mo ago
you can get it from the event object, or use userFor(event) that function is also detailed on the checks page
Solution
Shaun
Shaun•6mo ago
check {
val user = userFor(event)!!
val usage = usageService.checkUsage(user.id)
event.extraData["usageResponse"] = usage

when(usage) {
is UsageResponse.SuccessResponse -> pass()
is UsageResponse.FailureResponse -> fail(usage.reason)
}
}
check {
val user = userFor(event)!!
val usage = usageService.checkUsage(user.id)
event.extraData["usageResponse"] = usage

when(usage) {
is UsageResponse.SuccessResponse -> pass()
is UsageResponse.FailureResponse -> fail(usage.reason)
}
}
Shaun
ShaunOP•6mo ago
lgtm
No description
Shaun
ShaunOP•6mo ago
Can you dm slash commands to the bot?
gdude
gdude•6mo ago
yeah, if they're global
Shaun
ShaunOP•6mo ago
I need to reopen this as I'm back on this problem again. I need to send an ephemeral response to a command in some case, but the response needs to be customized and have some components on it. Othertimes the response will be public Would be neat if it were something like
respond {
if(checkSomething) {
content = "Ephemeral response"
ephemeral = true
} else {
content = "Public response"
ephemeral = false
}
}
respond {
if(checkSomething) {
content = "Ephemeral response"
ephemeral = true
} else {
content = "Public response"
ephemeral = false
}
}
interaction.respondEphemeral {
content = "You have gone over your free quota, next prices estimation in 1hr."
components = mutableListOf((ActionRowBuilder().apply {
premiumButton(BotSkus.LEGEND) {}
}))
}
interaction.respondEphemeral {
content = "You have gone over your free quota, next prices estimation in 1hr."
components = mutableListOf((ActionRowBuilder().apply {
premiumButton(BotSkus.LEGEND) {}
}))
}
This works but the extemsion already responds so it errors
gdude
gdude•6mo ago
the unsafe module provides a command type that allows you to handle responding to the interaction yourself it's not documented (yet) but it's not that complex to use
Shaun
ShaunOP•6mo ago
How do I stop kordex from responding
gdude
gdude•6mo ago
you have to use the unsafe commands
Shaun
ShaunOP•6mo ago
In the extension? within the action?
gdude
gdude•6mo ago
what? are you already using the unsafe module?
Shaun
ShaunOP•6mo ago
No
gdude
gdude•6mo ago
then you need to use it
Shaun
ShaunOP•6mo ago
Any docs/examples ?
gdude
gdude•6mo ago
it'll give you an unsafeSlashCommand which allows you to set (or disable) the initial response well you need to add the dependency
Shaun
ShaunOP•6mo ago
Is it your dep?
gdude
gdude•6mo ago
yeah, it's the same dep but the artifact ID is unsafe you need both
Shaun
ShaunOP•6mo ago
Unsafe feels such a strong word here 😂 manualSlashCommand
gdude
gdude•6mo ago
it's the correct word
Shaun
ShaunOP•6mo ago
how do I do this with toml add this?
kord-unsafe = { module = "com.kotlindiscord.kord.extensions:unsafe", version.ref = "kord-extensions" }
kord-unsafe = { module = "com.kotlindiscord.kord.extensions:unsafe", version.ref = "kord-extensions" }
gdude
gdude•6mo ago
yep
Shaun
ShaunOP•6mo ago
Dependency alias 'kord-unsafe' is not used in build scripts
gdude
gdude•6mo ago
you do need to actually use the alias in your build script yes
Shaun
ShaunOP•6mo ago
I really don't see the point in the toml file
gdude
gdude•6mo ago
libs.versions.kord.unsafe in your case I think it's more useful for multi-module projects and also for linters and dependency checkers eg dependabot on github
Shaun
ShaunOP•6mo ago
Should I be excluding dev.kord from unsafe too, while I use the newer kord version
gdude
gdude•6mo ago
checking no, it should be fine I'm not using API scoped dependencies in that module
Shaun
ShaunOP•6mo ago
Okay so how do I respond with an unsafe slash command? I see respondEphemeral and respondPublic, I'm guessing its those
gdude
gdude•6mo ago
no first of all, you need to set initialResponse in the command builder to InitialSlashCommandResponse.None in your case presumably in the action, you'll need to use one of the ack functions
Shaun
ShaunOP•6mo ago
If I used PublicAck then would the bot say its thinking, then I wouldn't need to acknlowedge further down
gdude
gdude•6mo ago
it uses deferPublicResponseUnsafe, I forget whether that shows the thinking message or not you can always test it
Shaun
ShaunOP•6mo ago
then how do I respond
gdude
gdude•6mo ago
you need to ack first the ack function also allows you to set an inital response content in the builder after you ack, you use the corresponding response function
Shaun
ShaunOP•6mo ago
initialResponse = InitialSlashCommandResponse.PublicAck doesn't ack?
gdude
gdude•6mo ago
it does, but if you use that then you may as well use a normal public command
Shaun
ShaunOP•6mo ago
It seems to work
gdude
gdude•6mo ago
the point of unsafe commands is to make it easier to change the response type dynamically but in doing so, you are accepting that things might not work how you expect them to if you don't do what discord expects
Shaun
ShaunOP•6mo ago
I want to always public ackknowledge and then respond privately/publicly Which is different to the publicSlashCommand beacuse it tries to ack twice
gdude
gdude•6mo ago
you simply can't do that
Shaun
ShaunOP•6mo ago
It works?
gdude
gdude•6mo ago
for now haha the ack types do matter to discord if you start with a public ack and then send an ephemeral response, that's undefined behaviour the API may break it
Shaun
ShaunOP•6mo ago
How do I set a command argument to be optional and set a default value?
gdude
gdude•6mo ago
defaultingXyz {
Shaun
ShaunOP•6mo ago
And what goes in the body
gdude
gdude•6mo ago
read the converters page in the docs
gdude
gdude•6mo ago
there's even an example in there
Want results from more Discord servers?
Add your server