serenity
serenity copied to clipboard
[Next] Message.category_id() and Message.Channel.category() always return None
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!("{:#?}", ¤t_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);
}
}
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.
This should be fixed on next now, although I currently cannot test it myself
I am now able to test myself, and can confirm it works
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