switchupcb / disgo

Create a Discord Bot in Go using this Discord API Wrapper. The next generation of Discord API Consumption.
Apache License 2.0
86 stars 2 forks source link

Example Request: Sharding #70

Open bkuhl opened 6 months ago

bkuhl commented 6 months ago

I'm on an ARM OSx device.

I'm not very familiar with golang and am trying to migrate my Discord bot to use your library. It's a sharded bot. I'm following the docs here and am encountering an error I don't understand. I was wondering if you'd be able to add a full example of how to run a sharded bot as I seem to be doing something wrong.

    bot := &disgo.Client{
        Authentication: disgo.BotToken(token),
        Config: &disgo.Config{
            Gateway: disgo.Gateway{
                GatewayPresenceUpdate: &disgo.GatewayPresenceUpdate{
                    Status: status,
                },
            },
        },
    }

    bot.Config.Gateway.ShardManager = new(shard.InstanceShardManager)
    s := bot.Config.Gateway.ShardManager

    s.Connect(bot)

I keep getting

bot-1  | panic: runtime error: invalid memory address or nil pointer dereference
bot-1  | [signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x3b5360]
bot-1  | 
bot-1  | goroutine 1 [running]:
bot-1  | github.com/switchupcb/disgo/shard.(*InstanceShardManager).Connect(0x400010e580, 0x400010e500)
bot-1  |        /go/pkg/mod/github.com/switchupcb/disgo/shard@v0.0.0-20230730073530-349d45654529/instance.go:86 +0x30
bot-1  | main.main()
bot-1  |        /app/main.go:64 +0x354
bot-1  | exit status 2
imide commented 5 months ago

unfortunately, i dont think this project is being maintained anymore... this looks really nice to use (and i find dgo really annoying to use so) i would help but im also brand new, a fucking moron, and idk where to start lol. 😭

bkuhl commented 5 months ago

We switched to use https://github.com/bwmarrin/discordgo and github.com/servusdei2018/shards for sharding and it's working great.

switchupcb commented 2 months ago

unfortunately, i dont think this project is being maintained anymore... — @imide

I was homeless.

if you'd be able to add a full example of how to run a sharded bot as I seem to be doing something wrong. — @bkuhl

The short term fix is to use `bot.Config.Gateway.ShardManager.SetNumShards(2)'.

The long term fix is that the Limit struct should not be nil.

I can implement this fix when I want or when I am paid to do so. However, you are using discordgo, so no payment is expected.

Here is what caused the issue.

Line 86 is a reference to totalShards = sm.Limit.RecommendedShards

The instantiated shard manager has a nil Limit struct.

When you call Connect, totalShards is not set, so it attempts to read from a nil pointer.

The shard test doesn't catch this issue because it calls bot.Config.Gateway.ShardManager.SetNumShards(2) which sets totalShards to 2.

The short term fix is to use that line of code and set the shards to a given number.

The long term fix is to make sure there is a limit set and consider whatever reason I had for not doing so.

https://github.com/servusdei2018/shards

Does this package make you use a lot of for loops to call code on each shard?

imide commented 2 months ago

I was homeless.

oh no! i hope you're back on your feet 👍🏼