bwmarrin / discordgo

(Golang) Go bindings for Discord
BSD 3-Clause "New" or "Revised" License
4.9k stars 786 forks source link

Message content is empty #1264

Open lambdaurora0 opened 1 year ago

lambdaurora0 commented 1 year ago

I tried to run the discord ping/pong example The bot doesn't respond even though it is online and has all the permissions.

Note also that after a log of m.Content, it is empty

What is the problem?

FedorLap2006 commented 1 year ago

Thanks for noticing this! The example requires a discordgo.IntentMessageContent to be present in s.Identify.Intents bitmap. However, probably we forgot to add it after the message content deadline was due.

The quick fix would be to add | discordgo.IntentMessageContent at the end of Identify.Intents assignment. Though, in the long term, I'll fix the example later this week.

Sandeep-Narahari commented 1 year ago
package main

import (
    "flag"
    "fmt"
    "os"
    "os/signal"
    "syscall"

    "github.com/bwmarrin/discordgo"
)

// Variables used for command line parameters
var (
    Token string
)

func init() {
    flag.StringVar(&Token, "t", "", "Bot Token")
    flag.Parse()
}

func main() {
    // Create a new Discord session using the provided bot token.
    dg, err := discordgo.New("Bot " + Token)
    if err != nil {
        fmt.Println("error creating Discord session,", err)
        return
    }
    dg.Identify.Intents = discordgo.IntentMessageContent

    // Register the messageCreate func as a callback for MessageCreate events.
    dg.AddHandler(messageCreate)

    // Just like the ping pong example, we only care about receiving message
    // events in this example.
    dg.Identify.Intents = discordgo.IntentsGuildMessages

    // Open a websocket connection to Discord and begin listening.
    err = dg.Open()
    if err != nil {
        fmt.Println("error opening connection,", err)
        return
    }

    // Wait here until CTRL-C or other term signal is received.
    fmt.Println("Bot is now running. Press CTRL-C to exit.")
    sc := make(chan os.Signal, 1)
    signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
    <-sc

    // Cleanly close down the Discord session.
    dg.Close()
}

// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the authenticated bot has access to.
//
// It is called whenever a message is created but only when it's sent through a
// server as we did not request IntentsDirectMessages.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
    // Ignore all messages created by the bot itself
    // This isn't required in this specific example but it's a good practice.

    if m.Author.ID == s.State.User.ID {
        return
    }
    // In this example, we only care about messages that are "ping".
    if m.Content == "ping" {
        // fmt.Println(m.Content)
        // fmt.Println(len(m.Content))
        s.ChannelMessageSend(m.ChannelID, "Hello")
    }
    fmt.Println(len(m.Content))

    // We create the private channel with the user who sent the message.
    channel, err := s.UserChannelCreate(m.Author.ID)
    if err != nil {
        // If an error occurred, we failed to create the channel.
        //
        // Some common causes are:
        // 1. We don't share a server with the user (not possible here).
        // 2. We opened enough DM channels quickly enough for Discord to
        //    label us as abusing the endpoint, blocking us from opening
        //    new ones.
        fmt.Println("error creating channel:", err)
        s.ChannelMessageSend(
            m.ChannelID,
            "Something went wrong while sending the DM!",
        )
        return
    }
    // Then we send the message through the channel we created.
    _, err = s.ChannelMessageSend(channel.ID, "Pong!")
    if err != nil {
        // If an error occurred, we failed to send the message.
        //
        // It may occur either when we do not share a server with the
        // user (highly unlikely as we just received a message) or
        // the user disabled DM in their settings (more likely).
        fmt.Println("error sending DM message:", err)
        s.ChannelMessageSend(
            m.ChannelID,
            "Failed to send you a DM. "+
                "Did you disable DM in your privacy settings?",
        )
    }
}
Cypaaa commented 1 year ago

Hi, I'm getting 2022/10/30 12:04:09 websocket: close 4014: Disallowed intent(s). when adding | discordgo.IntentMessageContent at the end of my s.Identify.Intents. Tried using only discordgo.IntentMessageContent as the intent and I've got the same error.

FedorLap2006 commented 1 year ago

Hi, I'm getting 2022/10/30 12:04:09 websocket: close 4014: Disallowed intent(s). when adding | discordgo.IntentMessageContent at the end of my s.Identify.Intents. Tried using only discordgo.IntentMessageContent as the intent and I've got the same error.

You need to have it approved & enabled in Developer Portal. It's privileged.

Cypaaa commented 1 year ago

Hi, I'm getting 2022/10/30 12:04:09 websocket: close 4014: Disallowed intent(s). when adding | discordgo.IntentMessageContent at the end of my s.Identify.Intents. Tried using only discordgo.IntentMessageContent as the intent and I've got the same error.

You need to have it approved & enabled in Developer Portal. It's privileged.

Yeah thanks I've just seen that it wasn't enabled by default, mb and thanks for your quick answering.