bwmarrin / discordgo

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

Using discord's client, pasting an image URL as the only content for a message causes GuildID to be missing from the Message struct #1575

Open azekeil opened 1 month ago

azekeil commented 1 month ago

As per the title: If you paste a URL to an image as a message in the discord client, the client will re-render the URL as the image. My guess is this causes the discord client to not populate the GuildID field of the Message struct - I'm not sure if that's a problem on discord client end or discordgo library's end. I've worked around this in my code with the following:

func EnsureGuildID(s *discordgo.Session, m *discordgo.Message) {
    if m.GuildID == "" {
        c, err := s.Channel(m.ChannelID)
        if err != nil {
            log.Fatalf("unable to get GuildID from Channel: %v", err)
        }
        m.GuildID = c.GuildID
        log.Printf("Added Guild %s to Message %s in Channel %s", m.GuildID, m.ID, m.ChannelID)
    }
}

Other messages don't appear to cause this issue, just image URLs that have been converted to images. I imagine this is going to be tough to get fixed upstream; do we want to fix this in the library for now?

Is GuildID something we can rely on being set in the Message struct? I see that the struct tag implies it can be omitted if empty:

type Message struct {
...
    // The ID of the guild in which the message was sent.
    GuildID string `json:"guild_id,omitempty"`
...
}