From b9816fab9690c4a9e33536de9793013fa2936dd4 Mon Sep 17 00:00:00 2001 From: toast Date: Sun, 19 Jan 2025 14:13:47 +1100 Subject: [PATCH] Experiment the role field in Serenity --- Cargo.lock | 7 ++-- Cargo.toml | 6 +++- cmds/Cargo.toml | 1 + cmds/src/dispatch.rs | 3 ++ cmds/src/dispatch/role_test.rs | 66 ++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 cmds/src/dispatch/role_test.rs diff --git a/Cargo.lock b/Cargo.lock index f1474d1..56433f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1473,6 +1473,7 @@ dependencies = [ "rand", "reqwest", "rustbot_lib", + "rustbot_tokens", "serde", "sysinfo", "uptime_lib", @@ -1668,9 +1669,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.135" +version = "1.0.136" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9" +checksum = "336a0c23cf42a38d9eaa7cd22c7040d04e1228a19a933890805ffd00a16437d2" dependencies = [ "itoa", "memchr", @@ -1702,7 +1703,7 @@ dependencies = [ [[package]] name = "serenity" version = "0.12.4" -source = "git+https://github.com/serenity-rs/serenity?branch=next#45fb401e38c61e84b08ed368ac61a1d92805d949" +source = "git+https://github.com/nwerosama/serenity?branch=update-emoji#ef416bf3ff8b9423bb5bba9e44d2ba3192e8db02" dependencies = [ "aformat", "arrayvec", diff --git a/Cargo.toml b/Cargo.toml index 14f710e..8e0419e 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,18 +24,22 @@ uptime_lib = "0.3.1" tokio = { version = "1.43.0", features = ["macros", "signal", "rt-multi-thread"] } reqwest = { version = "0.12.12", features = ["native-tls-vendored"] } rustbot_lib = { path = "library" } +rustbot_tokens = { path = "tsclient" } [dependencies] poise = { workspace = true } rustbot_cmds = { path = "cmds" } rustbot_events = { path = "events" } rustbot_lib = { workspace = true } -rustbot_tokens = { path = "tsclient" } +rustbot_tokens = { workspace = true } tokio = { workspace = true } [patch.crates-io] poise = { git = "https://github.com/serenity-rs/poise", branch = "serenity-next" } +[patch."https://github.com/serenity-rs/serenity"] +serenity = { git = "https://github.com/nwerosama/serenity", branch = "update-emoji" } + [features] production = ["rustbot_lib/production", "rustbot_events/production"] not_ready = ["rustbot_lib/not_ready"] diff --git a/cmds/Cargo.toml b/cmds/Cargo.toml index 30f94e0..90f1af2 100644 --- a/cmds/Cargo.toml +++ b/cmds/Cargo.toml @@ -8,6 +8,7 @@ poise = { workspace = true } rand = { workspace = true } reqwest = { workspace = true } rustbot_lib = { workspace = true } +rustbot_tokens = { workspace = true } serde = { workspace = true } sysinfo = { workspace = true } uptime_lib = { workspace = true } diff --git a/cmds/src/dispatch.rs b/cmds/src/dispatch.rs index 4d6fde7..4c00851 100644 --- a/cmds/src/dispatch.rs +++ b/cmds/src/dispatch.rs @@ -1,12 +1,14 @@ mod dev; mod eightball; mod ping; +mod role_test; mod uptime; pub use { dev::dev, eightball::eightball, ping::ping, + role_test::role_test, uptime::uptime }; @@ -21,6 +23,7 @@ macro_rules! collect { $crate::uptime(), // Unsorted mess $crate::eightball(), + $crate::role_test(), ] }; } diff --git a/cmds/src/dispatch/role_test.rs b/cmds/src/dispatch/role_test.rs new file mode 100644 index 0000000..f111e17 --- /dev/null +++ b/cmds/src/dispatch/role_test.rs @@ -0,0 +1,66 @@ +use { + poise::serenity_prelude::{ + Attachment, + CreateAttachment, + EmojiId, + RoleId + }, + rustbot_lib::{ + RustbotContext, + RustbotResult + } +}; + +fn extract_role_id(input: &str) -> Option { + let trimmed = input.trim(); + if trimmed.starts_with("<@&") && trimmed.ends_with(">") { + trimmed[3..trimmed.len() - 1].parse().ok() + } else { + trimmed.parse().ok() + } +} + +/// Role test +#[poise::command(slash_command, subcommands("create", "edit"), install_context = "Guild", interaction_context = "Guild")] +pub async fn role_test(_: RustbotContext<'_>) -> RustbotResult<()> { Ok(()) } + +/// Role test create +#[poise::command(slash_command, install_context = "Guild", interaction_context = "Guild")] +pub async fn create( + ctx: RustbotContext<'_>, + img: Attachment +) -> RustbotResult<()> { + ctx.defer().await?; + + let attachment = CreateAttachment::url(ctx.http(), img.url.to_string(), img.filename).await?; + + let e = ctx + .guild_id() + .unwrap() + .create_emoji(ctx.http(), "nep", &attachment.to_base64(), vec![RoleId::new(1292024065170608150)], None) + .await?; + + ctx.reply(format!("{} = {:?}", e.name, e.roles)).await?; + Ok(()) +} + +/// Role test edit +#[poise::command(slash_command, install_context = "Guild", interaction_context = "Guild")] +pub async fn edit( + ctx: RustbotContext<'_>, + emoji_id: String, + roles: String +) -> RustbotResult<()> { + ctx.defer().await?; + + let role_vec: Vec = roles.split(',').filter_map(extract_role_id).map(RoleId::new).collect(); + + let e = ctx + .guild_id() + .unwrap() + .edit_emoji(ctx.http(), EmojiId::new(emoji_id.parse().unwrap()), "nep", Some(role_vec), None) + .await?; + + ctx.reply(format!("{} = {:?}", e.name, e.roles)).await?; + Ok(()) +}