harmonyland / harmony

An easy to use Discord API Library for Deno.
https://harmony.mod.land
MIT License
311 stars 47 forks source link

[Bug] `guild.channels.keys()` should return keys only from its own guild #293

Closed greenya closed 2 years ago

greenya commented 2 years ago

What's the bug? guild.channels.keys() returns all keys (seems like from all guilds), while guild.channels.array() returns correct result.

How do we reproduce it? Assume we have messageCreate event, with msg arguments. We want to show list of all channels in the specific guild.

// assume "1111111111111111" this is correct discord user id (not shown here)
// and we send a command as private message from that user to our bot
if (msg.channel.isDM() && msg.author.id == '1111111111111111' && msg.content) {

    // ...

    // usage: list channels GUILD_ID
    const listChannelsArgs = msg.content.match(/list channels (\d+)/)
    if (listChannelsArgs) {
        const guildId = listChannelsArgs[1] // first match is at index 1
        const guild = await client.guilds.resolve(guildId)
        if (guild) {
            const channelsArray = await guild.channels.array()
            console.log('guild.channels.array()', channelsArray.length, channelsArray.map(c => [ c.guildID, c.id, c.name ]))
            // we expect each record to have same c.guildID -- WORKING AS EXPECTED

            const channelKeys = await guild.channels.keys()
            console.log('guild.channels.keys()', channelKeys.length, channelKeys)
            // we expect "channelKeys.length" to be same as previous "channelsArray.length" -- NOT WORKING

            // we continue to resolve each key, returned by "guild.channels.keys()"
            for (const key of channelKeys) {
                try {
                    const channel = await guild.channels.resolve(key)
                    if (channel) {
                        const e = channel.guildID == guild.id ? 'expected' : 'UNEXPECTED'
                        console.log(`channel ${channel.id} ${channel.name} has ${e} guildID`)
                    } else {
                        console.log(`channel ${key} cannot be resolved`)
                    }
                } catch (x) {
                    console.log(`channel ${key} cannot be resolved; error ${x}`)
                }
            }
        } else {
            console.log(`guild ${guildId} cannot be resolved`)
        }
    }

    // ...
}

What should have happened? Expected to have keys() all with correct guildID

What is actually happening? array() returns expected result, keys() returns extra channels (probably all)

What versions you're using?