diff options
-rw-r--r-- | Cargo.lock | 379 | ||||
-rw-r--r-- | Cargo.toml | 15 | ||||
-rw-r--r-- | migrations/20220112135205_reactionroles.sql | 9 | ||||
-rw-r--r-- | src/commands/mod.rs | 1 | ||||
-rw-r--r-- | src/commands/reactionroles.rs | 146 | ||||
-rw-r--r-- | src/handler.rs | 36 | ||||
-rw-r--r-- | src/main.rs | 24 | ||||
-rw-r--r-- | src/models.rs | 9 |
8 files changed, 616 insertions, 3 deletions
@@ -9,6 +9,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" [[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom 0.2.3", + "once_cell", + "version_check", +] + +[[package]] name = "ansi_term" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -61,6 +72,15 @@ dependencies = [ ] [[package]] +name = "atoi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616896e05fc0e2649463a93a15183c6a16bf03413a7af88ef1285ddedfa9cda5" +dependencies = [ + "num-traits", +] + +[[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -180,6 +200,21 @@ dependencies = [ ] [[package]] +name = "crc" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49fc9a695bca7f35f5f4c15cddc84415f66a74ea78eef08e90c5024f2b540e23" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccaeedb56da03b09f598226e25e80088cb4cd25f316e6e4df7d695f0feeb1403" + +[[package]] name = "crc32fast" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -189,6 +224,46 @@ dependencies = [ ] [[package]] +name = "crossbeam-channel" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e54ea8bc3fb1ee042f5aace6e3c6e025d3874866da222930f70ce62aceba0bfa" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b979d76c9fcb84dffc80a73f7290da0f83e4c95773494674cb44b76d13a7a110" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcae03edb34f947e64acdb1c33ec169824e20657e9ecb61cef6c8c74dcb8120" +dependencies = [ + "cfg-if", + "lazy_static", +] + +[[package]] +name = "crypto-mac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] name = "darling" version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -245,12 +320,38 @@ dependencies = [ ] [[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] name = "dotenv" version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" [[package]] +name = "either" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" + +[[package]] name = "encoding_rs" version = "0.8.30" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -333,6 +434,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0c8ff0461b82559810cdccfde3215c3f373807f5e5232b71479bff7bb2583d7" [[package]] +name = "futures-intrusive" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62007592ac46aa7c2b6416f7deb9a8a8f63a01e0f1d6e1787d5630170db2b63e" +dependencies = [ + "futures-core", + "lock_api", + "parking_lot", +] + +[[package]] name = "futures-io" version = "0.3.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -412,6 +524,7 @@ dependencies = [ "serde", "serde_json", "serenity 0.10.9 (registry+https://github.com/rust-lang/crates.io-index)", + "sqlx", "tokio", "tracing", "tracing-subscriber", @@ -441,6 +554,27 @@ name = "hashbrown" version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +dependencies = [ + "ahash", +] + +[[package]] +name = "hashlink" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7249a3129cbc1ffccd74857f81464a323a152173cdb134e0fd81bc803b29facf" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "heck" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" +dependencies = [ + "unicode-segmentation", +] [[package]] name = "hermit-abi" @@ -452,6 +586,22 @@ dependencies = [ ] [[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hmac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" +dependencies = [ + "crypto-mac", + "digest", +] + +[[package]] name = "http" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -587,6 +737,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9" [[package]] +name = "itertools" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9a9d19fa1e79b6215ff29b9d6880b706147f16e9b1dbb1e4e5947b5b02bc5e3" +dependencies = [ + "either", +] + +[[package]] name = "itoa" version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -653,6 +812,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" [[package]] +name = "md-5" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b5a279bb9607f9f53c22d496eade00d138d1bdcccd07d74650387cf94942a15" +dependencies = [ + "block-buffer", + "digest", + "opaque-debug", +] + +[[package]] name = "memchr" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -675,6 +845,12 @@ dependencies = [ ] [[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] name = "miniz_oxide" version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -725,6 +901,17 @@ dependencies = [ ] [[package]] +name = "nom" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +dependencies = [ + "memchr", + "minimal-lexical", + "version_check", +] + +[[package]] name = "ntapi" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1017,6 +1204,16 @@ dependencies = [ ] [[package]] +name = "redox_users" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64" +dependencies = [ + "getrandom 0.2.3", + "redox_syscall", +] + +[[package]] name = "regex" version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1316,6 +1513,19 @@ dependencies = [ ] [[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer", + "cfg-if", + "cpufeatures", + "digest", + "opaque-debug", +] + +[[package]] name = "sharded-slab" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1362,18 +1572,139 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" [[package]] +name = "sqlformat" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4b7922be017ee70900be125523f38bdd644f4f06a1b16e8fa5a8ee8c34bffd4" +dependencies = [ + "itertools", + "nom", + "unicode_categories", +] + +[[package]] +name = "sqlx" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "692749de69603d81e016212199d73a2e14ee20e2def7d7914919e8db5d4d48b9" +dependencies = [ + "sqlx-core", + "sqlx-macros", +] + +[[package]] +name = "sqlx-core" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "518be6f6fff5ca76f985d434f9c37f3662af279642acf730388f271dff7b9016" +dependencies = [ + "ahash", + "atoi", + "base64 0.13.0", + "bitflags", + "byteorder", + "bytes 1.1.0", + "chrono", + "crc", + "crossbeam-channel", + "crossbeam-queue", + "crossbeam-utils", + "dirs", + "either", + "futures-channel", + "futures-core", + "futures-intrusive", + "futures-util", + "hashlink", + "hex", + "hmac", + "indexmap", + "itoa 1.0.1", + "libc", + "log", + "md-5", + "memchr", + "once_cell", + "parking_lot", + "percent-encoding", + "rand 0.8.4", + "rustls 0.19.1", + "serde", + "serde_json", + "sha-1", + "sha2", + "smallvec", + "sqlformat", + "sqlx-rt", + "stringprep", + "thiserror", + "tokio-stream", + "url", + "uuid", + "webpki 0.21.4", + "webpki-roots 0.21.1", + "whoami", +] + +[[package]] +name = "sqlx-macros" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38e45140529cf1f90a5e1c2e561500ca345821a1c513652c8f486bbf07407cc8" +dependencies = [ + "dotenv", + "either", + "heck", + "once_cell", + "proc-macro2", + "quote", + "sha2", + "sqlx-core", + "sqlx-rt", + "syn", + "url", +] + +[[package]] +name = "sqlx-rt" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8061cbaa91ee75041514f67a09398c65a64efed72c90151ecd47593bad53da99" +dependencies = [ + "once_cell", + "tokio", + "tokio-rustls 0.22.0", +] + +[[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] +name = "stringprep" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ee348cb74b87454fff4b551cbf727025810a004f88aeacae7f85b87f4e9a1c1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] name = "strsim" version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] name = "syn" version = "1.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1515,6 +1846,17 @@ dependencies = [ ] [[package]] +name = "tokio-stream" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] name = "tokio-util" version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1693,12 +2035,24 @@ dependencies = [ ] [[package]] +name = "unicode-segmentation" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" + +[[package]] name = "unicode-xid" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" [[package]] +name = "unicode_categories" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39ec24b3121d976906ece63c9daad25b85969647682eee313cb5779fdd69e14e" + +[[package]] name = "untrusted" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1724,6 +2078,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] +name = "uuid" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7" + +[[package]] name = "uwl" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1870,6 +2230,15 @@ dependencies = [ [[package]] name = "webpki-roots" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aabe153544e473b775453675851ecc86863d2a81d786d741f6b76778f2a48940" +dependencies = [ + "webpki 0.21.4", +] + +[[package]] +name = "webpki-roots" version = "0.22.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "552ceb903e957524388c4d3475725ff2c8b7960922063af6ce53c9a43da07449" @@ -1878,6 +2247,16 @@ dependencies = [ ] [[package]] +name = "whoami" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524b58fa5a20a2fb3014dd6358b70e6579692a56ef6fce928834e488f42f65e8" +dependencies = [ + "wasm-bindgen", + "web-sys", +] + +[[package]] name = "winapi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -29,11 +29,15 @@ features = [ "model", "standard_framework", "utils", + "collector", ] [dependencies.poise] git = "https://github.com/kangalioo/poise" branch = "master" +features = [ + "collector" + ] [dependencies.tokio] version = "1" @@ -49,3 +53,14 @@ features = [ "derive" ] [dependencies.serde_json] version = "1" + +[dependencies.sqlx] +version = "0.5" +features = [ + "runtime-tokio-rustls", + "postgres", + "uuid", + "chrono", + "migrate", + "macros", +] diff --git a/migrations/20220112135205_reactionroles.sql b/migrations/20220112135205_reactionroles.sql new file mode 100644 index 0000000..303e025 --- /dev/null +++ b/migrations/20220112135205_reactionroles.sql @@ -0,0 +1,9 @@ +-- Add migration script here +CREATE TABLE reaction_roles ( + id SERIAL PRIMARY KEY, + channel_id TEXT NOT NULL, + message_id TEXT NOT NULL, + guild_id TEXT NOT NULL, + reaction TEXT NOT NULL, + role_id TEXT NOT NULL +); diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 50636a3..2121532 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -2,3 +2,4 @@ pub mod meta; pub mod pony; pub mod actions; pub mod osu; +pub mod reactionroles; diff --git a/src/commands/reactionroles.rs b/src/commands/reactionroles.rs new file mode 100644 index 0000000..53aa794 --- /dev/null +++ b/src/commands/reactionroles.rs @@ -0,0 +1,146 @@ +use std::{time::Duration, str::FromStr}; + +use crate::{Context, Error}; +use poise::serenity_prelude::{self as serenity, ReactionType, Emoji, ArgumentConvert}; +use ::serenity::framework::standard::{Args, Delimiter}; + +#[cfg(debug_assertions)] +async fn allowed_to_create_roles(ctx: Context<'_>) -> Result<bool, Error> { + if ctx.author().id.0 == 118455061222260736u64 { + Ok(true) + } else { + Ok(false) + } +} + +#[cfg(not(debug_assertions))] +async fn allowed_to_create_roles(ctx: Context<'_>) -> Result<bool, Error> { + if let Some(guild) = ctx.guild() { + if guild.owner.id == ctx.author().id { + Ok(true) + } else { + let member = guild.member(ctx.discord(), ctx.author().id)?; + let member_permissions = member.permissions().await?; + + Ok(member_permissions.manage_roles()) + } + Ok(false) +} +} + +/// Manages reaction role menus +/// +/// Subcommands: +/// - init +/// - add +/// - remove +#[poise::command(prefix_command, ephemeral, + check="allowed_to_create_roles")] +pub async fn rroles(ctx: Context<'_>) -> Result<(), Error> { + ctx.say("Maybe you meant to request help for this? Try `/help rroles`").await?; + Ok(()) +} + +/// Initializes a reaction role menu in the given channel +/// +/// Usage: +/// rroles init <#channel> +/// Example: +/// rroles init #get-roles +#[poise::command(prefix_command, ephemeral, + check="allowed_to_create_roles")] +pub async fn init(ctx: Context<'_>, + #[description = "The channel to create a new role menu in"] + channel: serenity::ChannelId) -> Result<(), Error> { + let mut rolemenu_msg = channel.send_message(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Menu"); + e.description("I haven't been initialized yet! Hold on just a second"); + e + }); + m + }).await?; + + let mut menu = ctx.send(|m| { + m.embed(|e| { + e.title("Reaction Role Setup"); + e.description("Welcome to the setup menu! I'm going to help guide you through setting up your reaction roles.\n\nFirst, what should the title of your menu?"); + e + }); + m + }).await?.unwrap().message().await?; + + if let Some(title) = ctx.author().clone().await_reply(ctx.discord()).timeout(Duration::from_secs(10)).await { + rolemenu_msg.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title(title.content.clone()); + e + }); + m + }).await?; + + menu.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title("Reaction Role Setup"); + e.description(format!("Great! I've set the title of your menu to `{}`.\n\nNext, let's add some roles! Reply to this message with a list of role names and reactions, like this:\n\n:female_sign::She/Her,:male_sign::He/Him", title.content.clone())); + e + }); + m + }).await?; + } else { + ctx.say("No response within 10 seconds").await?; + return Ok(()); + } + { + let pool = ctx.data().pg.lock().unwrap().clone(); + if let Some(roles) = ctx.author().clone().await_reply(ctx.discord()).timeout(Duration::from_secs(30)).await { + let mut args = Args::new(&roles.content, &[Delimiter::Single(',')]); + + let mut rolelist_formatted = String::from("Choose the appropriate reaction to gain the role!\n"); + + for a in args.iter::<String>() { + if let Ok(tuple) = a { + let split_str: Vec<&str> = tuple.split(':').collect(); + let reaction = ReactionType::from_str(split_str[0])?; + let role_name = split_str[1]; + if let Some(role) = ctx.guild().unwrap().role_by_name(role_name) { + sqlx::query!("INSERT INTO reaction_roles (channel_id, message_id, guild_id, reaction, role_id) VALUES ($1, $2, $3, $4, $5)", rolemenu_msg.channel_id.0.to_string(), rolemenu_msg.id.0.to_string(), ctx.guild_id().unwrap().0.to_string(), reaction.to_string(), role.id.0.to_string()).execute(&pool).await?; + rolemenu_msg.react(ctx.discord(), reaction.clone()).await?; + rolelist_formatted.push_str(&format!("{} - {}\n", get_reactiontype_display(&reaction), role.name.clone())); + } else { + ctx.say(format!("Invalid role provided: {}", role_name)).await?; + return Ok(()); + } + } + } + let title = rolemenu_msg.clone().embeds[0].title.clone().unwrap_or("Reaction Role Menu".to_string()); + rolemenu_msg.edit(ctx.discord(), |m| { + m.embed(|e| { + e.title(title); + e.description(rolelist_formatted); + e + }); + m + }).await?; + } else { + ctx.say("No response within 30 seconds").await?; + return Ok(()); + } + } + + Ok(()) +} + +fn get_reactiontype_display(rt: &ReactionType) -> String { + match rt { + ReactionType::Unicode(emote) => emote.clone(), + ReactionType::Custom { id, name, .. } => { + if let Some(name) = name { + format!("<:{}:{}>", name, id) + } else { + format!("<{}>", id) + } + }, + _ => String::new(), + } +} diff --git a/src/handler.rs b/src/handler.rs new file mode 100644 index 0000000..933bd72 --- /dev/null +++ b/src/handler.rs @@ -0,0 +1,36 @@ +use poise::serenity_prelude as serenity; + +use crate::{Data, Error}; +use crate::models::ReactionRole; + +pub async fn event_handler(ctx: &serenity::Context, + event: &poise::Event<'_>, + data: &Data, + ) -> Result<(), Error> { + { + let pool = data.pg.lock().unwrap().clone(); + match event { + poise::Event::ReactionAdd { add_reaction } => { + let rrole = sqlx::query_as!(ReactionRole, "SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", add_reaction.message_id.0.to_string(), add_reaction.emoji.to_string()).fetch_one(&pool).await?; + let member = ctx.http.get_member(rrole.guild_id.parse::<u64>()?, add_reaction.user_id.unwrap().0).await?; + let member_roles = member.roles; + let role_id = serenity::RoleId(rrole.role_id.parse::<u64>()?); + if member_roles.contains(&role_id) { + ctx.http.remove_member_role(member.guild_id.0, member.user.id.0, role_id.0, Some("Reaction Role Menu")).await?; + } else { + ctx.http.add_member_role(rrole.guild_id.parse::<u64>()?, add_reaction.user_id.unwrap().0, rrole.role_id.parse::<u64>()?, Some("Reaction Role")).await?; + } + + let dm_chan = add_reaction.user_id.unwrap().create_dm_channel(&ctx.http).await?; + dm_chan.say(ctx, format!("Toggled the role!")).await?; + + add_reaction.delete(&ctx.http).await?; + + }, + _ => (), + } + } + + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index bb686d2..021886f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,17 @@ -use std::time::Duration; +use std::{time::Duration, sync::Mutex}; use dotenv::dotenv; +use sqlx::{PgPool, postgres::PgPoolOptions}; type Error = Box<dyn std::error::Error + Send + Sync>; type Context<'a> = poise::Context<'a, Data, Error>; mod commands; +mod handler; +mod models; pub struct Data { - + pg: Mutex<PgPool>, } /// Show help menu @@ -63,6 +66,13 @@ async fn main() { commands::osu::osup(), commands::osu::osubm(), + + poise::Command { + subcommands: vec![ + commands::reactionroles::init(), + ], + ..commands::reactionroles::rroles() + }, ], on_error: |error| Box::pin(on_error(error)), pre_command: |ctx| { @@ -85,6 +95,7 @@ async fn main() { ], ..Default::default() }, + listener: |ctx, event, _, data| Box::pin(handler::event_handler(ctx, event, data)), ..Default::default() }; @@ -92,7 +103,14 @@ async fn main() { .token(std::env::var("DISCORD_TOKEN").unwrap_or("BAD-TOKEN".into())) .user_data_setup(move |_ctx, _ready, _framework| { Box::pin(async move { - Ok(Data {}) + let pool = PgPoolOptions::new() + .max_connections(5) + .connect(&std::env::var("DATABASE_URL").unwrap_or("postgres://postgres@localhost/glitch".to_string())) + .await + .expect("Couldn't connect to postgresql"); + Ok(Data { + pg: Mutex::new(pool) + }) }) }) .options(options) diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..4b2e7bb --- /dev/null +++ b/src/models.rs @@ -0,0 +1,9 @@ +#[derive(Debug)] +pub struct ReactionRole { + pub id: i32, + pub channel_id: String, + pub message_id: String, + pub guild_id: String, + pub reaction: String, + pub role_id: String, +} |