Skip to content

Panic when getting a guild member's permissions. #64

@holedaemon

Description

@holedaemon

Description
When attempting to get a guild member's permissions a panic occurs. From my testing, it only occurs when using the instance of GuildMember passed with a new message. If you manually call a member with Guild(...).Member(...) and use that, the panic does not occur.

What did you do?
Within my message handler function, I call the Guild and Channel with the appropriate client methods. Then, I call *message.Member.PermissionsIn(...) and the panic occurs.

What did you expect to see?
A guild member's permissions in a channel printed to stdout.

What did you see instead?
A panic resulting from the call to PermissionsIn().

What version of the module are you using ?
v0.15.0

Below is the code I used to test:

package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"

	"github.com/skwair/harmony"
)

type bot struct {
	client *harmony.Client
}

func main() {
	b := &bot{}
	token := os.Getenv("HARMONY_TEST_TOKEN")
	client, err := harmony.NewClient(token)

	if err != nil {
		fmt.Println(err)
		return
	}

	b.client = client
	b.client.OnMessageCreate(b.message)

	if err := b.client.Connect(context.Background()); err != nil {
		fmt.Println(err)
		return
	}
	defer b.client.Disconnect()

	close := make(chan os.Signal, 1)
	signal.Notify(close, os.Interrupt)
	<-close
}

func (b *bot) message(m *harmony.Message) {
	if m.Content == "!perms" {
		guild, err := b.client.Guild(m.GuildID).Get(context.Background())

		if err != nil {
			fmt.Println(err)
			return
		}

		channel, err := b.client.Channel(m.ChannelID).Get(context.Background())

		if err != nil {
			fmt.Println(err)
			return
		}

		var permissions int

		// With GuildMember from API
		member, err := b.client.Guild(m.GuildID).Member(context.Background(), m.Author.ID)

		if err != nil {
			fmt.Println(err)
			return
		}

		permissions = member.PermissionsIn(guild, channel)
		fmt.Println("API:", permissions)

		// With GuildMember from Message
		permissions = m.Member.PermissionsIn(guild, channel)
		fmt.Println("Message:", permissions)

	}
}

And the output from that:

λ go run .
API:  2146958591
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x66f60d]

goroutine 39 [running]:
github.com/skwair/harmony.computeBasePermissions(0xc000214680, 0xc000107f40, 0xc000167930)
        C:/Users/Eli/go/pkg/mod/github.com/skwair/[email protected]/guild_permission.go:7 +0x3d
github.com/skwair/harmony.(*GuildMember).PermissionsIn(0xc000107f40, 0xc000214680, 0xc0001dab40, 0x2)
        C:/Users/Eli/go/pkg/mod/github.com/skwair/[email protected]/guild_member.go:27 +0x3c
main.(*bot).message(0xc00009c018, 0xc000108a80)
        C:/Users/Eli/projects/go/harmony-test/main.go:71 +0x419
github.com/skwair/harmony.messageCreateHandler.handle(0xc00005abf0, 0x69dae0, 0xc000108a80)
        C:/Users/Eli/go/pkg/mod/github.com/skwair/[email protected]/events.go:329 +0x48
created by github.com/skwair/harmony.(*Client).handle
        C:/Users/Eli/go/pkg/mod/github.com/skwair/[email protected]/dispatch.go:351 +0xe0
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions