Send message to specific channel from slash command

I'm so sorry for having to ask this, but... how do you send a message to a specific channel from within a slash command? I believe I'm missing a huge gotcha here.
10 Replies
Rhys
Rhys•2y ago
@Freakspot Could you expand a bit more on what you're looking to do? Is it A) Responding to a slash command in that channel that it was used in B) Ignoring responding to a slash command in the channel it was used in and instead sending that response in a different channel, i.e it was invoked in #help and sends the response in #logs
Freakspot
Freakspot•2y ago
I'm looking to do B-ish: Send a message to a channel #logs, then reply to the slash command in the channel it was used in. Replying is easy, but sending to #logs - no idea.
Rhys
Rhys•2y ago
Does something like this work? I can post more context to it if needed but you just get the channel you want to send the message in from the guild the interaction is used in, verify it's a text channel, then call .send on it
const target = await interaction.guild.channels.cache.get('id')
if (target?.isTextBased()) {
await target.send(...)
}
...reply to interaction here
const target = await interaction.guild.channels.cache.get('id')
if (target?.isTextBased()) {
await target.send(...)
}
...reply to interaction here
Freakspot
Freakspot•2y ago
Oh, so you can get the id for the channel from the interaction parameter and then use send. Let me try that real quick, but it looks promising for sure.
const target= await interaction.guild?.channels.cache.get('id');
const target= await interaction.guild?.channels.cache.get('id');
Okay, so that does work! Thank you so far. Unfortunately, the following code throws some errors:
if (target?.isTextBased()) {
await target.send()
}
if (target?.isTextBased()) {
await target.send()
}
as Property 'isTextBased' does not exist on type 'GuildBasedChannel'. Property 'isTextBased' does not exist on type 'CategoryChannel'. and Property 'send' does not exist on type 'GuildBasedChannel'. Property 'send' does not exist on type 'CategoryChannel'. Edit: I was able to solve the errors by replacing isTextBased() with isText() I suppose this is an issue with the typescript setup? I knew I should've just used JS to keep it simple... 😄 Additionally, I realized that I'm not just trying to send a text message, but create a thread in a forum channel. Would this approach still work with that? I'll figure that bit out myself! Don't wanna overstay my welcome.
Rhys
Rhys•2y ago
Typescript / JavaScript doesn’t really matter here although typescript helps a lot with these types of things, can you post how you’re getting the channel you’re trying to send a message in? It looks like it’s trying to post in category
Freakspot
Freakspot•2y ago
Oh I just pasted the ID in directly for now.
Rhys
Rhys•2y ago
If you’re using Sapphire v3 I don’t actually think you can make a post in a forum channel yet as it doesn’t support those
Freakspot
Freakspot•2y ago
Oh! I just started using Sapphire yesterday, so I guess I'm lucky since v4 seems to be dropping today!? 😄
Rhys
Rhys•2y ago
Yeah good timing on that Wait for that to be release and then that might help, ping me again if it doesn’t and I can take a look
Freakspot
Freakspot•2y ago
Thank you! 🙂 Managed to get this to work with your initial input and v4! Thank you so much, @Rhys. 🥰
Want results from more Discord servers?
Add your server