#How to make the bot send a dm
1 messages · Page 1 of 1 (latest)
<@&987246527741304832> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
To make your bot send a direct message to a user on Discord, you can use the following code snippet in your Java Discord bot:
User user = event.getAuthor(); // Get the user to send the message to
user.openPrivateChannel().queue(channel -> {
channel.sendMessage("Your message here").queue();
});
In this code, event.getAuthor() retrieves the user who sent the original message that triggered the bot. Then, openPrivateChannel() opens a private channel between the bot and the user, and sendMessage() sends the specified message to that channel.
Make sure you have the necessary permissions and intents enabled in your bot's configuration to send direct messages. Also, remember to handle any potential exceptions or errors that may occur during this process.
If you are still facing issues or need further assistance, feel free to ask for help on our Java Q&A Discord server.
EmbedBuilder eb = new EmbedBuilder();
User user = event.getUser();
eb.setTitle("Staff Application accepted");
eb.setDescription("""
Congratulations your staff application on Block Tune has been accepted!!
You will receive a DM from a staff recruiter soon and they will be giving you your roles in the server.
""");
eb.setColor(Color.orange);
eb.setThumbnail("https://cdn.discordapp.com/attachments/1135319391765008495/1262432550241042536/Logo_2.png?ex=669c822e&is=669b30ae&hm=e6c38282ef72e2cd01cc2284d061b61124e986f7b979aadfe7ba3beac1608736&");
eb.setFooter("Powered by Block Tune");
user.openPrivateChannel().queue();
}```
openPrivateChannel() only gives u the channel. u still have to send the message over that channel
u know, channel.sendMessage(message) etc
tldr, look at the example the bot gave u
yeah but as what do I define channel?
TextChannel channel = …
or what
lookup the method openPrivateChannel(), it tells u the exact type it returns
or use var
or simple chain
openPrivateChannel().sendMessage(...)...queue()
that doesn’t exist
but will try
it does
declaration: package: net.dv8tion.jda.api.entities, interface: PrivateChannel
its a rest action. so u have to flatmap
author.openPrivateChannel()
.flatMap(channel -> channel.sendMessage("Hello"))
.queue();
if that concept is new to u, u should probably step back right now
as JDA is built on top of this
its everywhere
its classic async code
restactions = futures
so they have flatMap as "and then"
and map as "transform"
I am using jda for a long time already just never did a anything with dms
and queue at the very end sends out the final action chain
okay. then u should be familiar with rest actions 👍
ik
lemme take a look and see if it eorks
works
so. openPrivateChannel gives u a RestAction<PrivateChannel>
now it sends the dm to the person who clicked the button the dm
I however wanted it to send the dm to the one who submitted an application so basically the one who interacted with the very first button
any idea how
well, open the channel on the right User
can't really help u decipher ur existing code logic without seeing it
code file
the java file please
not class 🙂
and then please dont discard the bot who uploads it as gist, thanks
I uploaded your attachments as Gist.
sorry 😅
could u guide us through the code please. what's going on
basically the first part (MessageReceived event) is just the embed to select either the staff or the freelancer application. then if you click on the button for staff application a modal appears for the user to fill out. after submitting it sends an ebmbed in a set channel for the owner to review. If he clicks on accept application button the I want the bot to send a dm to the user that applied. same thing when the owner clicks the denied button
and I strugle on the dm part
everything else works
so u have to memorize the original user throughout ur flow
could also use the queue(Consumer). would be a bit more descriptive too, since nothing is actually getting flattened
for example by putting his user id into the component id
and then later retrieving the user with that id again
like, ur button id
its freelancer or staff
instead, also add the user id to it
freelancer;123463228484
then, when the button is later clicked, u getId()
split it into the freelancer and the user id
then retrieve that user
but the user id is different every time isn't it? or am I understanding smth wrong
jda.retrieveUser(12345..)
.flatMap(user -> user.openPrivateChannel())
.flatMap(channel -> channel.sendMessage("huhu"))
.queue()
so? u have the right user id available at the start of ur flow
so that's where u put the user id into ur button ids
then later u retrieve it and act on it
hmm lemme try
its the way how discord/jda wants u to transport/memorize data throughout the async flow
u put it into the component ids
like, suppose u want to make a dialog to create a button that will say hello to a user u selected in the dialog
then that button somehow needs to memorize which user its tied to
i. e. the dynamic information from the dialog that spawned it
so that later, when someone clicks the button a year later, ur code still knows who this button was supposed to say hello to
so instead of the button having just an id of "hello_button", u would also attach the user id to it
"hello_button;1428528472"
then, when the button is clicked, u can extract the information back from there and use it
why jda. tho?
theres plenty of options to retrieve an user. that's just one of them
somehow I am not capable of doing what you told me
could you maybe give me a step by step guide ?
which part of what i said is unclear to u?
where in ur code is the user u want to dm to?
and where exactly is the point in ur code where u want to do the dm thing?
what's the flow like between those two points
how and where I get the user id from and where in my code i put it how
youll need to store the ID of the user who created the app somewhere
when someone submits an app, it sounds like your code creates a message with a button. you could have a Map, which you would store the ID of the message with the button (as the key) and the ID of the user who submitted the app (as a value)
yeah but how do I put that in my code
you'd declare the map as a field variable. then, where you handle the app submitting code (in onModalInteraction), you would grab the ID of the button & the ID of the user who created the event
first of all, step back and tell me where in ur code u have the user u want to send a dm to
write a System.out.println("Hello ..."); with the user u want to later DM to
where in ur code do u have hands on the user?
u cant continue with the rest if u dont understand where in ur code the user u want to work with is located at...
so lets do that first
atm I have the hand on the user no where
well normally I would get the user from the very top where i made the buttons in the onMessageReveived EVent
please describe in words which user u want to DM
so that we can spot the point in ur flow where this user would be available
since u seem to not know
Okay I shared my code with you right?
so in the very first lines us the onMessageReceivedEvent where I have made two buttons for two applications
i want to dm the user that interacts with the button
so the user who clicks the freelancer/staff app buttons should be DMed?
or the user who send the +setup message?
yes
no that is just to send the whole embed with both buttons
so what happens when someone clicks the button? a modal pops up and then eventually the user clicks on submit on the modal, right?
and thats when u want to send the DM?
or already when the staff/freelancer button is clicked?
oh, the modal creates even more, right?
when a user clicks the button staff/freelancer which opens a modal. After submitting a modal it sends another embed in a different channel with the values of the modal. If the another person clicks on the button attechted to the embed send by the modal I want the bot to send a dm to the user that clicked the staff/freelancer button
ah i see
okay. so heres the thing
when the freelancer/staff button is clicked, u have the member u want to DM here:
notice the "when the freelancer/staff button is clicked"
not ur other button
but thats the user u want to DM
yes
also, u have him in the modal
since he is the one submitting the modal
so here:
thats also ur DM-user
okay understood
the flow from here is that this piece of code sends some messages with button in that other channel u mentioned
the accept-button
the button is then clicked by someone else, like a moderator or so
correct
and the flow continues here:
i.e. in ur button handler again
but this time in the other if block
in ur button handler, u have the possibility to access the so called component ID of the button
ur doing that at the very top already
String butttonId = event.getButton().getId();
now, this ID can be choosen freely by u
which u did here for example:
so nothing would hold u back from storing actual information in there
other than just the name of ur button
such as for example the id of the user u want to DM
Button.success("accept;" + member.getId(), "Accept Application")...
ohh like that that makes sense
now ur button id is sth like accept;1291249129412393 in practice
okay and then I retrieve that id how?
so when its later clicked and u end up here: https://i.imgur.com/i8Fgctv.png
u can now extract that id
String userId = buttonId.split(";")[1];
after extraction, u need to somehow retrieve a proper user object from that id
like a User or Member
for that, u can write jda.retrieveUserById(userId)
well jda. doesn’t work I tried it like that already
it does
u can get hands on JDA everywhere
event.getJda()
for example
event.getJda().retrieveUserById(userId)
oh right
u could also retrieve a member via the guild
doesnt matter
server.retrieveMemberById(userId)
but if u go with former, u can also message users who arent in ur guild anymore
okay I think I understand now
now, this is a rest action. so u have the user now in ur async "future"
RestAction<User>
and can flatMap from there
event.getJda().retrieveUserById(userId)
.flatMap(User::openPrivateChannel)
.flatMap(channel -> channel.sendMessage("huhu"))
.queue();
why twice : between User::openPrivateChannel?
its called a method reference
its the same as writing user -> user.openPrivateChannel()
👌
@onyx quartz
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure 👍
worked perfectly fine after some changes! thank you very much 