#How to make the bot send a dm

1 messages · Page 1 of 1 (latest)

onyx quartz
#

I am trying to let my bot send a member a dm to a user but I don't know hoe to do it I am very confused..

valid brookBOT
#

<@&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>.

#
TJ-Bot
I am trying to let my bot send a member a dm to a user but I don't know hoe to do it I am very confused..

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.

onyx quartz
#
            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();


        }```
viral bluff
#

u know, channel.sendMessage(message) etc

#

tldr, look at the example the bot gave u

onyx quartz
#

TextChannel channel = …

#

or what

viral bluff
#

or use var

#

or simple chain

#

openPrivateChannel().sendMessage(...)...queue()

onyx quartz
viral bluff
#

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"

onyx quartz
viral bluff
#

and queue at the very end sends out the final action chain

viral bluff
onyx quartz
#

lemme take a look and see if it eorks

#

works

viral bluff
#

so. openPrivateChannel gives u a RestAction<PrivateChannel>

onyx quartz
#

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

viral bluff
#

well, open the channel on the right User

#

can't really help u decipher ur existing code logic without seeing it

viral bluff
#

please just drag drop the code file here

#

else i can't view it on mobile

onyx quartz
onyx quartz
viral bluff
#

the java file please

#

not class 🙂

#

and then please dont discard the bot who uploads it as gist, thanks

onyx quartz
#

how do i get the java file?

valid brookBOT
onyx quartz
#

ohh

#

makes sense now

viral bluff
#

could u guide us through the code please. what's going on

onyx quartz
# viral bluff 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

viral bluff
#

so u have to memorize the original user throughout ur flow

versed laurel
viral bluff
#

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

onyx quartz
viral bluff
#
jda.retrieveUser(12345..)
 .flatMap(user -> user.openPrivateChannel())
 .flatMap(channel -> channel.sendMessage("huhu"))
 .queue()
viral bluff
#

so that's where u put the user id into ur button ids

#

then later u retrieve it and act on it

onyx quartz
#

hmm lemme try

viral bluff
#

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

viral bluff
#

theres plenty of options to retrieve an user. that's just one of them

onyx quartz
#

could you maybe give me a step by step guide ?

viral bluff
#

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

onyx quartz
#

how and where I get the user id from and where in my code i put it how

versed laurel
#

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)

onyx quartz
versed laurel
viral bluff
#

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

onyx quartz
onyx quartz
viral bluff
#

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

onyx quartz
#

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

viral bluff
#

or the user who send the +setup message?

onyx quartz
viral bluff
#

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?

onyx quartz
viral bluff
#

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

onyx quartz
#

yes

viral bluff
#

also, u have him in the modal

#

since he is the one submitting the modal

#

so here:

#

thats also ur DM-user

onyx quartz
#

okay understood

viral bluff
#

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

onyx quartz
#

correct

viral bluff
#

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")...

onyx quartz
#

ohh like that that makes sense

viral bluff
#

now ur button id is sth like accept;1291249129412393 in practice

onyx quartz
#

okay and then I retrieve that id how?

viral bluff
#

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)

onyx quartz
#

well jda. doesn’t work I tried it like that already

viral bluff
#

it does

#

u can get hands on JDA everywhere

#

event.getJda()

#

for example

#

event.getJda().retrieveUserById(userId)

onyx quartz
#

oh right

viral bluff
#

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

onyx quartz
#

okay I think I understand now

viral bluff
#

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();
onyx quartz
#

why twice : between User::openPrivateChannel?

viral bluff
#

its called a method reference

#

its the same as writing user -> user.openPrivateChannel()

onyx quartz
#

ahh

#

I see I still have a bit to learn😅

#

but thank you I will try when I get home

viral bluff
#

👌

valid brookBOT
#

@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 👍

onyx quartz