arikawa icon indicating copy to clipboard operation
arikawa copied to clipboard

v3: Inconsistent channel permission values for large guilds between sessions

Open Cynosphere opened this issue 1 year ago • 9 comments

Every other session ends up with channels in a specific large guild not having permissions (returning 0). The three other smaller guilds the account is in do not have this issue.

I have triaged with another one of my bot accounts in that guild running Dysnomia, targeting that account to check permissions across all channels and it shows correct values.

Additional context: ningen is used

Cynosphere avatar Nov 20 '23 01:11 Cynosphere

How are you getting channel permissions? What's your code?

diamondburned avatar Nov 20 '23 02:11 diamondburned

for _, channel := range guildChannels {
  if channel.Type != discord.GuildText && channel.Type != discord.GuildAnnouncement {
    continue
  }

  private := false

  if channel.ParentID.IsValid() {
    category, err := client.ChannelStore.Channel(channel.ParentID)
    if err == nil {
      perms := discord.CalcOverwrites(*guild, *category, *selfMember)
      private = !perms.Has(discord.PermissionViewChannel)
    }
  }

  if private {
    perms := discord.CalcOverwrites(*guild, channel, *selfMember)
    private = !perms.Has(discord.PermissionViewChannel)
  }

  if private && !withPrivate {
    continue
  }

  // ...
}

Cynosphere avatar Nov 20 '23 02:11 Cynosphere

CalcOverwrites will not try to fetch missing permissions from the given channel. You must use client.Permissions to fetch that. Ideally, CalcOverwrites should not even be used at all.

diamondburned avatar Nov 20 '23 02:11 diamondburned

client.Permissions is consistently returning 0 now when it should be returning 401295005309505 for most of the channels.

Cynosphere avatar Nov 20 '23 02:11 Cynosphere

What does client.Client.Permissions return? (Make sure that client.Client is specifically of type *api.Client.)

diamondburned avatar Nov 20 '23 03:11 diamondburned

*api.Client does not seem to have a Permissions function

Cynosphere avatar Nov 20 '23 03:11 Cynosphere

Sorry, try client.Client.Channel and check its permissions. Also note that these functions don't handle carrying over category permissions.

diamondburned avatar Nov 20 '23 03:11 diamondburned

The permission results seems to be inverted with using !channel.SelfPermissions.Has(discord.PermissionViewChannel), where it thinks the private channels are the ones that the account can see. This is also way too slow for my liking.

I did make my own version of client.Permissions to debug with, and the root cause seems to be guild.Roles being empty.

Cynosphere avatar Nov 20 '23 04:11 Cynosphere

Hmm, so https://github.com/diamondburned/arikawa/blob/v3.3.3/state/state.go#L310 should probably error out when Roles == nil then, which makes sense. (Not the function itself, but this particular branch.)

diamondburned avatar Nov 20 '23 04:11 diamondburned