Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(*Session).State is broken in all versions after 0.20.3 #812

Closed
NaniteFactory opened this issue Aug 30, 2020 · 4 comments
Closed

(*Session).State is broken in all versions after 0.20.3 #812

NaniteFactory opened this issue Aug 30, 2020 · 4 comments

Comments

@NaniteFactory
Copy link

I've been using discordgo for months, and these state caches became all empty since release of v0.21.1.

session.State.Guilds
session.State.Channels
session.State.Guilds.Members

Can someone take a look into this please? Thanks.

@CarsonHoffman
Copy link
Collaborator

Are you using intents? If so, what intents are you requesting?

@NaniteFactory
Copy link
Author

Well... I'm not aware of whether I'm using intents or not, because none of my codes explicitly request any intent.
Below is how I get my session connect to Discord. I guess this may be somehow relatable?

// Open shards sessions and add event handlers to them.
func (d *Discord) Open(handlers ...interface{}) error {
	d.mu.Lock()
	defer d.mu.Unlock()
	// Get Gateway Bot.
	gateway, err := discordgo.New("Bot " + d.botToken)
	if err != nil {
		return err
	}
	st, err := gateway.GatewayBot()
	if err != nil {
		return err
	}
	// Shard into sessions.
	d.shards = make([]*discordgo.Session, st.Shards)
	for i := 0; i < st.Shards; i++ {
		d.shards[i], err = discordgo.New("Bot " + d.botToken)
		if err != nil {
			return err
		}
		d.shards[i].ShardID = i
		d.shards[i].ShardCount = st.Shards
		d.shards[i].StateEnabled = true // just in case
	}
	// Add handlers.
	for _, sess := range d.shards {
		for _, handler := range handlers {
			sess.AddHandler(handler)
		}
	}
	var errOpen error
	{ // Open ws connections.
		wg := sync.WaitGroup{}
		for _, sess := range d.shards {
			wg.Add(1)
			go func(sess *discordgo.Session) {
				if err := sess.Open(); err != nil {
					errOpen = err
				}
				sess.StateEnabled = true // just in case
				wg.Done()
			}(sess)
		}
		wg.Wait()
	}
	// Close if fail.
	if errOpen != nil {
		wg := sync.WaitGroup{}
		for _, sess := range d.shards {
			wg.Add(1)
			go func(sess *discordgo.Session) {
				_ = sess.Close()
				wg.Done()
			}(sess)
		}
		wg.Wait()
	}
	return errOpen
}

@CarsonHoffman
Copy link
Collaborator

It's expected that this code will not work for fetching and caching members. This was described in the release notes for v0.21, as well as the FAQ.

@NaniteFactory
Copy link
Author

NaniteFactory commented Aug 30, 2020

Okay, so I went to https://discord.com/developers/applications/ , ticked both Presence Intent and Server Members Intent, and just added this line of code.

sess.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged | discordgo.IntentsGuildPresences | discordgo.IntentsGuildMembers)

And it's working again as usual in the lastest current master.
sess.State.Channels and sess.State.Guilds are just fine. I might have mistaken those for something else.
Sorry for my ignorance and thanks a lot!! @CarsonHoffman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants