serenity icon indicating copy to clipboard operation
serenity copied to clipboard

guild_update uses new name in old_data

Open ghost opened this issue 5 years ago • 3 comments

On the docs, it says:

Provides the guild's old full data (if available) and the new, albeit partial data.

Actions

[dependencies.serenity]
version = "0.8"
default_features = true
pub struct Handler;

impl EventHandler for Handler {
  fn guild_update(
    &self,
    ctx: Context,
    old_guild: Option<Arc<RwLock<Guild>>>,
    new_guild: PartialGuild,
  ) {
    if let Some(old_guild) = old_guild {
      let data = old_guild.read();
      if new_guild.name == data.name {
        println!("Guild Updated: {} to {}", data.name, new_guild.name);
        return;
      }

      println!("Guild Renamed: {} to {}", data.name, new_guild.name);
    }
  }
}

I renamed my Discord server from Old_Name to New_Name.

Expected

If old_guild isn't available, I don't expect it to print anything. If old_guild is available, I expect it to print out: "Guild Renamed: OId_Name to New_Name"

Actual

It prints: "Guild Updated: New_Name to New_Name"

ghost avatar Mar 18 '20 14:03 ghost

I propose closing this issue. The guild_update signature seems to have changed.

Cargo.toml

[dependencies]
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
serenity = { version = "0.10", default-features = false, features = ["client", "gateway", "rustls_backend", "model"] }

main.rs

use serenity::{async_trait, model::prelude::*, prelude::*};

struct Handler;

#[async_trait]
impl EventHandler for Handler {
    async fn guild_update(&self, _ctx: Context, new_data: PartialGuild) {
        println! {"new_data.name:\n{:#?}", new_data.name};
    }
}

#[tokio::main]
async fn main() {
    let token = "REDACTED";

    let mut client = Client::builder(&token)
        .event_handler(Handler)
        .await
        .expect("Err creating client");

    if let Err(why) = client.start().await {
        println!("Client error: {:?}", why);
    }
}

There is no old_guild in 0.10.8. In my tests, new_data.name contains the new name of the updated Discord channel.

rasm47 avatar Jul 01 '21 13:07 rasm47

There is no old_guild in 0.10.8.

There is, it is just gated behind the cache feature.

arqunis avatar Jul 01 '21 13:07 arqunis

I made a better (but still unsuccessful) attampt at replicating the issue, this time with cache.

Cargo.toml

[dependencies]
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
serenity = { version = "0.10", default-features = false, features = ["client", "gateway", "rustls_backend", "model", "cache"] }

main.rs

    async fn guild_update(&self, _ctx: Context, old_data: Option<Guild>, new_data: PartialGuild) {
        if let Some(od) = old_data {
            println! {"old_data.name: {:#?}", od.name};
        } else {
            println! {"old_data was None"};
        }
        println! {"new_data.name: {:#?}", new_data.name};
    }

When I changed the name of my Discord server from changeme to changed, the console printed:

old_data.name: "changeme"
new_data.name: "changed"

rasm47 avatar Jul 01 '21 19:07 rasm47

I cannot reproduce either Screenshot_20220917_123803

According to the code, the old guild data is retrieved before the update event is incorporated into the cache, which also seems correct

https://github.com/serenity-rs/serenity/blob/2eea7d792615c25ed904d08ec5931d935a314e9a/src/client/dispatch.rs#L604-L609

I propose we close this issue. It can be reopened if someone manages to reproduce after all

kangalio avatar Sep 17 '22 10:09 kangalio