Discord Direct Messaging

Hi all,

Is it possible through Bubble to be able to directly message a user on Discord? I already utilise a plugin which allows me to grab the user’s Discord ID etc through OAuth verification and this is stored under User Data.

I’d like it so an administrator could have a message typed, and then press ‘DM user’ or something for example, and the user would receive a message on Discord with the contents of the message typed.

Any help would be appreciated.

OE Solutions.

BUMP
This is needed as a matter of priority and any assistance would be greatly appreciated.
OE Solutions.

BUMP. Interested too.

I’d suggest y’all to looking into Discord’s API Documentation.

2 Likes

Got it working now, thank you.

@cliffwoodjames I’d absolutely love to find out how you did it. I can’t work it out from the manual. Could you describe the steps, or at least point to the specific items?

Thank you in advance!

1 Like

Hey andrew,

I’ve actually just built a Discord-integrated system for a community based website, with automatic role syncing and everything. Was a fun project.

We accomplished this just using Bubble’s API Connector. I highly recommend using that as opposed to any Discord plugin out on the market right now. Maybe it’s time I make a Discord plugin…? Hmmm… Lol

If you have any specific questions I can try to help, but I’ll give you the basics for how we setup the connection to user accounts.

When creating the API call, for Authentication, I just used None or self-handled - because I had a Data structure setup where I saved users Discord Code, Discord Token, Refresh Token, Expiration Date, and Discord ID of the user.

To be fully honest, it does get slightly complicated with the setting up. Essentially, when a user logs in, they are prompted to link Discord. They are then sent to Discord’s oAuth login page, located: https://discord.com/api/oauth2/authorize?response_type=code&client_id=934517161634988062&scope=connections%20email%20identify%20guilds%20guilds.join%20messages.read&state=15773059ghq9183habn&redirect_uri=[WEBSITE REDIRECT HERE]&prompt=consent - The redirect then goes to a page I setup specifically for Discord linking, where I get a confirmation of “Do you want to link your Discord account” and the user clicks yes or no.

If yes, well, you have to create an API call on the API connector to https://discord.com/api/oauth2/token as a POST request. This will kick back with a JSON body which I store everything it kicks back with (besides Scope, I don’t save that).

Honestly, it can be pretty complicated. But, it all makes sense in the API doc’s.

Now, this is what all I setup to do user-authentication and to be able to do actions on behalf of the user (such as force-join or force-leave a server, which my client wanted to be able to force a user into their Discord upon acceptance into the community).

If you were just wanting to do stuff on behalf of a bot, such as send DMs, you wouldn’t need to do any of what I just said… Instead, you could:


Create the API calls.

I will show you how to setup the 2 API calls required to send a DM.

Yes, there will need to be 2 different calls. This is because the first call you need to make is to get a Channel ID of a DM, which is a call of it’s own, and then use that Channel ID to actually send a message.

In your API connector, in your Discord section, make a new call. I titled mine “Message - Step 1” - and I use it as Action, with a Data-Type of JSON.

It is a POST request, sent to https://discord.com/api/users/@me/channels

For Headers, you need 2.
Key: Content-Type, Value: application/json
Key: Authorization, Value: Bot [BOT TOKEN HERE]

Where I have Bot Token Here, you will need to paste your Bot’s token. If you don’t have one, or don’t know what I mean, read this:

Now, for body type, you are going to use JSON.

Here is the very easy JSON body I used:
{
"recipient_id":<User ID>
}

Here is a screenshot of this API call, incase I was unclear.

Also take note of what I have as Private and not private.

API Call #2, sending the message.

Alright, similar stuff, little different. The call URL is a POST to https://discord.com/api/channels/[Channel ID]/messages - It is important to leave the [Channel ID] as is. I’m assuming you’ve used the API Connector before, so you know that is a dynamic field that you can edit when you go to make the call in a workflow.

Same headers, Content-Type: application/json and Authorization: Bot [BOT TOKEN] both set to Private.

Body type is JSON, and the body is just as easy.
{
"content":"<Message>"
}

Again, here is a picture of this call on my end.
Note: My Message - Step 2 actually used an Embed, so that is why this is not called Message - Step 2


Actually using the calls.

Okay, now, how do we use these together? Easy.

First, you’ll trigger Step 1, with the Discord User ID that you want to DM.
image

Then, using your Step 2 call, send the Channel ID of “Result’s of step 1’s body id”
image

Again, my Step 2 is slightly different than the example. Sorry. But, the Channel ID will be the same.


Conclusion

In conclusion, just know some users have their privacy setup to not receive DMs from Bots. I know there is a way to catch that error and tell the front-end user. I haven’t actually done this myself yet. But it is possible. I hope this helps. I kinda rambled a bit, but let me know if you have any specific questions.

5 Likes

@draked123 Wow, if I could buy you a beer, I would! I’ll digest all this in a while and give it a go. Thank you so much :beers:

Haha, thanks!

I certainly didn’t do a good job of making it easy read, so sorry about that lol. Let me know if I need to clarify something, I’m happy to help.

Welllll, I’m not a developer and haven’t ever used the API connector in Bubble… So this is pretty intimidating to be honest. I’m not sure I can do this, and even if I can, I wouldn’t be confident I could maintain it. The first functionality you described sounds very useful for me, but if you say it’s complex, then I have to forget it! The DM functionality sounds more achievable, but still complex. I need to learn the basics of API calls first, or use a Discord plugin that can get/use the channel ID of a DM…

I’d be more than happy to sit down and walk you through all of it this weekend if you have spare time. I can walk you through how it all works, and what all is talking to each other. While complex to setup, it’s one of those “Set it up and forget about it” - as I can help you build recursive back-end workflows that automatically regenerates user’s tokens on expiration.

Sadly my Saturday is pretty busy after about 10 AM EST, but if you want to do earlier than that I can.

I may also throw together a very basic Discord Bot plugin (and release it for free, of course, I love giving back to the community lol). I will try my hand at it right now. My thought is, I could pretty easily do:

  • Role updating
  • Nickname Changins
  • DMing Users

I may be a little over-confident in my abilities. I can easily do APIs, but Plugin creation I am iffy at.

That would be awesome if you could walk me through it :slight_smile: I’m in Europe, but let’s see if we can find a time tomorrow or Sunday. My Discord is FarFutureFox#2270. I’m sure you’d breeze a plugin!

Hi @draked123, would you be able to provide a screenshot of your POST call to discord? Sorry to bring back an old thread, but after much searching this is the only post that even kind of gets me close to getting basic “logging in with Discord” working through the Bubble API connector.

I have my setup such so that the GET + code response from Discord is all working well. But at the point where I have to use API connector to set up a POST call with the code back to the token endpoint, I can’t configure it correctly so that it doesn’t return an error.

Any help on this would be MASSIVELY appreciated!!
Thanks,
David

Sure, no worries. Here is my POST call to the token endpoint:

Thanks so much for the fast reply! I have to pat myself on the back too, as it turns out that I was able to craft the exact same thing for my app in the time since I posted and you responded.

The POST is now going through correctly, and I’m getting the response. All that’s left for me now is figuring out how to do a simple GET request to get the username and email. Any tips on that front?

I feel like this should be easy compared to the POST but Im struggling haha

Hi! I am working on something similar but I need the user to be able to send and see messages in a discord server without being logged in. I already kind of have something set up using webhooks and a discord plugin and have it so that the users profile and profile picture from the bubble website is used as the name and picture when the message is sent. My only issue is being able to see the incoming messages in the server as well as switching channels. Currently I just have a button and a input field that lets me send a message to one channel and I see nothing unless I open the discord application on my computer, any advice? Even if I have to go in a completely different direction I am just playing with ideas right now any advice would be greatly appreciated