serenity icon indicating copy to clipboard operation
serenity copied to clipboard

[Next] Message.category_id() and Message.Channel.category() always return None

Open tiritto opened this issue 2 years ago • 1 comments

EDIT: It looks like the issue is not directly caused by the category_id() function itself, but rather something further down the line. See my 2nd comment for more information. I've left my original message intact to not disturb the flow for those, who might have seen the issue prior to my edit.

It appears that category_id() from serenity::model::channel::Message doesn't work. I've been trying to access it in multiple ways, but it just always returns "None".

Current behavior: The function returns None regardless whether channel is placed within a category or not.

Expected behavior: The function returns ChannelId of its parent category, if any.

Affected branches: next

Additional information:

  • Cache is properly initialized and does contain categories in question;
  • rustc -V: rustc 1.54.0 (a178d0322 2021-07-26)

Affected code:

struct Handler;

#[async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        let current_category = msg.category_id(ctx.cache); // Also tested with &ctx.cache
        println!("{:#?}", &current_category); // <- Returns "None"
        match current_category {
            Some(id) => { println!("This category has ID: {}", id.as_u64()) },
            None => println!("This channel has no category!") // <- Always triggered
        }
    }
}

#[tokio::main]
async fn main() {
    let mut client =
        Client::builder(TOKEN) // declaration removed on purpose
            .intents(GatewayIntents::GUILD_MEMBERS
                | GatewayIntents::GUILDS
                | GatewayIntents::GUILD_MESSAGES)
            .event_handler(Handler)
            .await
            .expect("Err creating client");
    if let Err(why) = client.start().await {
        println!("Client error: {:?}", why);
    }
}

tiritto avatar Sep 24 '21 06:09 tiritto

After trying to come out with some workaround while this issue persists, I've found out that the issue is doesn't come from Message.category_id() function directly, but from something else along the way. It looks like it, because after trying to get ChannelCategory via Message.Channel.category() (simplified for readability) I get the exact same problem of always being left out with None.

Affected code fragment:

    async fn message(&self, ctx: Context, msg: Message) {
        let category = &msg.channel(&ctx).await.map_or(None, |channel| channel.category());

        match category {
            Some(channel) => { println!("This category has ID: {}", channel.id.as_u64()) },
            _ => println!("This channel has no category!") // <- Always gets triggered
        }
    }

I can also confirm that the category itself is present in the cache, just as expected. I've checked that using the following syntax, and print results came out just as expected.

let test = &ctx.cache.category(ChannelId(441639080309293056)).unwrap();
println!("{:#?}", &test);

The category ID used in the example is also the very same category in which I've run all my tests.

tiritto avatar Sep 24 '21 09:09 tiritto

This should be fixed on next now, although I currently cannot test it myself

kangalio avatar Sep 12 '22 09:09 kangalio

I am now able to test myself, and can confirm it works Screenshot_20220917_101741 The printed channel ID is indeed the category channel ID, I checked. Code used:

async fn message(&self, ctx: Context, msg: Message) {
    dbg!(msg.category_id(&ctx).await);
    dbg!(msg.channel(&ctx).await.unwrap().guild().unwrap().parent_id);
}

This issue can be closed now

kangalio avatar Sep 17 '22 08:09 kangalio