Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send private message with discordgo

Tags:

go

discord

I would like to have a discord bot reply to a message made in a public channel via a private message.

I am able to detect whether a channel is private or not using the following code from the FAQ:

func isTheChannelTheMessageWasSentInPrivate(s *discordgo.Session, m *discordgo.MessageCreate) {
    channel, err := s.State.Channel(m.ChannelID)
    if err != nil {
        astilog.Fatal(err)
        return
    } else if m.Author.ID == s.State.User.ID {
        return
    }
    channelIsPrivate := strconv.FormatBool(channel.IsPrivate)
    print("Channel ID: " + m.ChannelID + ". Is it private? " + channelIsPrivate + "\n")
}

And I can reply to a message on the same channel it was received using this code:

func recieveMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
    s.ChannelMessageSend(m.ChannelID, "Reply!")
}

But I can't figure our how to get the ChannelID of a user's direct message channel from the Message object that is available upon receiving a message.

like image 587
Increasingly Idiotic Avatar asked Apr 08 '18 11:04

Increasingly Idiotic


People also ask

How do you send a private message on Discord?

Assuming you, and the other user are in the same server, this should be relatively simple. Open the Discord Channel and tap on their profile icon. A small box will appear letting you type a private message. Simple. Now, this only works if you both are in the same groups.

How do you send a link to someone on Discord?

A page will appear with a shareable link. Copy and paste it in a text or message (on another platform) send it to the person you’d like to chat with. If they click the link and accept you can private message them as we explained above, or chat with them in your Discord channel.

Can you message on Discord without being friends?

One of the great features that Discord has to offer is the ability to chat without trolls, spam, and just plain excessively annoying discourse. With that said, one of the drawbacks is the ability to message anyone you’d like whenever you’d like. Officially, Discord doesn’t give us the option to chat with another user unless we’re friends.

How do I stop unwanted messages on Discord?

If someone has been sending you unsolicited messages on Discord, you can make use of the block feature to keep them from continuing to do so. After you block them, they won’t be able to send you messages or friend requests until you unblock them.


1 Answers

The session struct has a method UserChannelCreate(recipientID string), which returns the DM channel for given userID. Don't mind the 'Create', if a DM channel already exists, it will be reused.

like image 151
tsdtsdtsd Avatar answered Sep 23 '22 10:09

tsdtsdtsd