#Why is session.State.User.ID nil?

13 messages · Page 1 of 1 (latest)

viral fog
#
// Create command handler (Command struct created below)
func CommandHandler(s *dis.Session, i *dis.InteractionCreate) {
    s.InteractionRespond(i.Interaction, &dis.InteractionResponse{
        Type: dis.InteractionResponseChannelMessageWithSource,
        Data: &dis.InteractionResponseData{
            Content: "Congrats! You've used the test command!",
        },
    })
}

func main() {

    client := client_init()           // Calls "dis.New("Bot" + TOKEN)" and retuns the result as "client".
    set_intents(client)               // Sets bot intents
    fmt.Println(client.State.User.ID) // ERROR IS HERE! Why is the ID nil? The session didn't throw an error when it initialized.

    var testCommand dis.ApplicationCommand = dis.ApplicationCommand{Name: "testCommand", Description: "This is a test command"} // Command struct
    client.AddHandler(CommandHandler)                                                                                           // Registering handler
    client.ApplicationCommandCreate(client.State.User.ID, "", &testCommand)                                                     // Registering command

    err := client.Open() // Open the connection to Discord
    if err != nil {
        log.Fatal(err)
    }

    defer client.Close()

    keep_alive() // Prevents the script from immediatly closing the connection and allows me to manually clost the connecction with "CTRL+C" in vs code
}

Error Message:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x6ea499]

goroutine 1 [running]:
main.main()
        /home/eulerian/Projects/[PROJECT_NAME]/main.go:24 +0x59
exit status 2

I'm not sure why the aforementioned ID is a nil (or invalid) pointer. Any other tips on structuring slash commands is also appreciated, as the example on github is literally over 500 lines long and difficult to read.

#

Imports aren't incuded because I don't have Nitro and the message was too ong. I imported the discordgo package as "dis"

narrow fossil
#

Because s.State.User is populated when READY is dispatched

#

For sake of god do NOT autoregister commands due to ratelimits (they might be short but they can be harsh, up to 24h)

#

Make a text command available only to you (note that discord gives message content if bot was mentioned in message)

viral fog
narrow fossil
#

So the s.State.User != nil assumption is always true there

#

(And yeah, just make another function with (shard *discordgo.Session, event discordgo.MessageCreate) signature and s.AddHandler it)

viral fog
#

Okay, I think I gotcha. I believe I'm getting lost in all the structs and how they interact with one another.

viral fog
#

Okay so I ended up solving my issue a bit differently. The main issue was, as you said, s.State.User.ID was not yet populated. The fix was simply moving my command and command handler registration to happen AFTER the websocket connection to Discord (the s.Open func). Also, it's worth noting that you will need to reinvite the Bot after you make changes to slash commands in this way apparently. You could also probably add the command handler before the websocket connection, as it does not require s.State.User.ID

#

UPDATED CODE SNIPPET:

#

// Create command struct (Command handler created below)
var testCommand dis.ApplicationCommand = dis.ApplicationCommand{Name: "test", Description: "This is a test command"}

// Create command handler (Command struct created above) (Remember to make response handlers for your commands!)
func CommandHandler(s *dis.Session, i *dis.InteractionCreate) {
    s.InteractionRespond(i.Interaction, &dis.InteractionResponse{
        Type: dis.InteractionResponseChannelMessageWithSource,
        Data: &dis.InteractionResponseData{
            Content: "Congrats! You've used the test command!",
        },
    })
}

// Create "ready" handler. Ensures bot initializations is successful.
func ReadyHandler(s *dis.Session, event *dis.Ready) {
    fmt.Println("Bot is ready!")
}

func main() {

    client := client_init() // Calls "dis.New("Bot" + TOKEN)" and retuns the result as
    set_intents(client)     // Sets bot intents

    client.AddHandler(ReadyHandler)

    err := client.Open() // Open the connection to Discord
    if err != nil {
        log.Fatal(err)
    }

    // Register Application Command Handler and the Appication Command itself
    client.AddHandler(CommandHandler)
    registered_command, err = client.ApplicationCommandCreate(client.State.User.ID, "", &testCommand)
    if err != nil {
        fmt.Println(err)
    }

    defer client.Close()

    keep_alive() // Prevents the script from immediatly closing the connection and allows me to manually clost the connecction with "CTRL+C" in vs code
}