From d6a2cb5c8be3160cabe498f6ac607d777c339dbf Mon Sep 17 00:00:00 2001 From: moonleay Date: Wed, 21 Feb 2024 09:58:26 +0100 Subject: [PATCH] feat: started with basic commands, added dependencies, started working on lavalink impl --- .vscode/settings.json | 3 +- Cargo.lock | 395 ++++++++++++++++++++++++-- Cargo.toml | 2 + src/commands/info.rs | 21 ++ src/commands/mod.rs | 4 +- src/commands/ping.rs | 10 - src/commands/play.rs | 38 +++ src/commands/stop.rs | 21 ++ src/handler/lavalink_event_handler.rs | 8 + src/handler/mod.rs | 1 + src/main.rs | 63 +++- src/util/config.rs | 10 +- src/util/mod.rs | 2 +- 13 files changed, 532 insertions(+), 46 deletions(-) create mode 100644 src/commands/info.rs delete mode 100644 src/commands/ping.rs create mode 100644 src/commands/play.rs create mode 100644 src/commands/stop.rs create mode 100644 src/handler/lavalink_event_handler.rs create mode 100644 src/handler/mod.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index ec17418..285dc0b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "rust-analyzer.linkedProjects": [ ".\\Cargo.toml" - ] + ], + "rust-analyzer.showUnlinkedFileNotification": false } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 3fc0caf..0dac61a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -32,6 +41,12 @@ dependencies = [ "libc", ] +[[package]] +name = "arc-swap" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" + [[package]] name = "arrayvec" version = "0.7.4" @@ -52,6 +67,23 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "async-tungstenite" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3609af4bbf701ddaf1f6bb4e6257dff4ff8932327d0e685d3f653724c258b1ac" +dependencies = [ + "futures-io", + "futures-util", + "log", + "pin-project-lite", + "rustls-pki-types", + "tokio", + "tokio-rustls 0.25.0", + "tungstenite 0.21.0", + "webpki-roots 0.26.1", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -178,8 +210,10 @@ checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", "serde", + "wasm-bindgen", "windows-targets 0.52.0", ] @@ -399,6 +433,7 @@ checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" dependencies = [ "futures-channel", "futures-core", + "futures-executor", "futures-io", "futures-sink", "futures-task", @@ -421,6 +456,17 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.30" @@ -477,6 +523,19 @@ dependencies = [ "byteorder", ] +[[package]] +name = "generator" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc16584ff22b460a382b7feec54b23d2908d858152e5739a120b949293bd74e" +dependencies = [ + "cc", + "libc", + "log", + "rustversion", + "windows", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -521,7 +580,7 @@ dependencies = [ "futures-core", "futures-sink", "futures-util", - "http", + "http 0.2.11", "indexmap", "slab", "tokio", @@ -552,6 +611,17 @@ dependencies = [ "itoa", ] +[[package]] +name = "http" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b32afd38673a8016f7c9ae69e5af41a58f81b1d31689040f2f1959594ce194ea" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + [[package]] name = "http-body" version = "0.4.6" @@ -559,7 +629,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ceab25649e9960c0311ea418d17bee82c0dcec1bd053b5f9a66e265a693bed2" dependencies = [ "bytes", - "http", + "http 0.2.11", "pin-project-lite", ] @@ -586,7 +656,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.11", "http-body", "httparse", "httpdate", @@ -606,11 +676,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", - "http", + "http 0.2.11", "hyper", - "rustls", + "rustls 0.21.10", "tokio", - "tokio-rustls", + "tokio-rustls 0.24.1", ] [[package]] @@ -677,6 +747,36 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lavalink-rs" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87676c01f7c63f255899cc2672f11a1d7d6848787cf0e6abbc6b79c839cf9f24" +dependencies = [ + "arc-swap", + "async-tungstenite", + "dashmap", + "futures", + "http 1.0.0", + "oneshot", + "parking_lot", + "reqwest", + "serde", + "serde_json", + "serde_qs", + "tokio", + "tracing", + "typemap_rev", + "url", + "version_check", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "levenshtein" version = "1.0.5" @@ -722,6 +822,29 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "loom" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5" +dependencies = [ + "cfg-if", + "generator", + "pin-utils", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.7.1" @@ -779,6 +902,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-conv" version = "0.1.0" @@ -819,12 +952,27 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "oneshot" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f6640c6bda7731b1fdbab747981a0f896dd1fedaf9f4a53fa237a04a84431f4" +dependencies = [ + "loom", +] + [[package]] name = "option-ext" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "parking_lot" version = "0.12.1" @@ -957,6 +1105,50 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regex" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.5", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.2", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + [[package]] name = "reqwest" version = "0.11.24" @@ -969,7 +1161,7 @@ dependencies = [ "futures-core", "futures-util", "h2", - "http", + "http 0.2.11", "http-body", "hyper", "hyper-rustls", @@ -981,7 +1173,7 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", - "rustls", + "rustls 0.21.10", "rustls-pemfile", "serde", "serde_json", @@ -989,7 +1181,7 @@ dependencies = [ "sync_wrapper", "system-configuration", "tokio", - "tokio-rustls", + "tokio-rustls 0.24.1", "tokio-util", "tower-service", "url", @@ -997,7 +1189,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-streams", "web-sys", - "webpki-roots", + "webpki-roots 0.25.4", "winreg", ] @@ -1025,7 +1217,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" name = "rustendo" version = "0.1.0" dependencies = [ + "chrono", "confy", + "lavalink-rs", "serde", "serde_json", "serenity", @@ -1053,10 +1247,24 @@ checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" dependencies = [ "log", "ring", - "rustls-webpki", + "rustls-webpki 0.101.7", "sct", ] +[[package]] +name = "rustls" +version = "0.22.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e87c9956bd9807afa1f77e0f7594af32566e830e088a5576d27c5b6f30f49d41" +dependencies = [ + "log", + "ring", + "rustls-pki-types", + "rustls-webpki 0.102.2", + "subtle", + "zeroize", +] + [[package]] name = "rustls-pemfile" version = "1.0.4" @@ -1066,6 +1274,12 @@ dependencies = [ "base64", ] +[[package]] +name = "rustls-pki-types" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "048a63e5b3ac996d78d402940b5fa47973d2d080c6c6fffa1d0f19c4445310b7" + [[package]] name = "rustls-webpki" version = "0.101.7" @@ -1076,6 +1290,23 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustls-webpki" +version = "0.102.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faaa0a62740bedb9b2ef5afa303da42764c012f743917351dc9a237ea1663610" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" + [[package]] name = "ryu" version = "1.0.16" @@ -1091,6 +1322,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "scopeguard" version = "1.2.0" @@ -1157,6 +1394,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_qs" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0431a35568651e363364210c91983c1da5eb29404d9f0928b67d4ebcfa7d330c" +dependencies = [ + "percent-encoding", + "serde", + "thiserror", +] + [[package]] name = "serde_spanned" version = "0.6.5" @@ -1225,6 +1473,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "skeptic" version = "0.13.7" @@ -1277,6 +1534,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "1.0.109" @@ -1364,6 +1627,16 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.34" @@ -1444,7 +1717,18 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" dependencies = [ - "rustls", + "rustls 0.21.10", + "tokio", +] + +[[package]] +name = "tokio-rustls" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "775e0c0f0adb3a2f22a00c4745d728b479985fc15ee7ca6a2608388c5569860f" +dependencies = [ + "rustls 0.22.2", + "rustls-pki-types", "tokio", ] @@ -1456,11 +1740,11 @@ checksum = "212d5dcb2a1ce06d81107c3d0ffa3121fe974b73f068c8282cb1c32328113b6c" dependencies = [ "futures-util", "log", - "rustls", + "rustls 0.21.10", "tokio", - "tokio-rustls", - "tungstenite", - "webpki-roots", + "tokio-rustls 0.24.1", + "tungstenite 0.20.1", + "webpki-roots 0.25.4", ] [[package]] @@ -1547,6 +1831,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -1570,11 +1884,32 @@ dependencies = [ "byteorder", "bytes", "data-encoding", - "http", + "http 0.2.11", "httparse", "log", "rand", - "rustls", + "rustls 0.21.10", + "sha1", + "thiserror", + "url", + "utf-8", +] + +[[package]] +name = "tungstenite" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ef1a641ea34f399a848dea702823bbecfb4c486f911735368f1f137cb8257e1" +dependencies = [ + "byteorder", + "bytes", + "data-encoding", + "http 1.0.0", + "httparse", + "log", + "rand", + "rustls 0.22.2", + "rustls-pki-types", "sha1", "thiserror", "url", @@ -1682,6 +2017,12 @@ version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4bf03e0ca70d626ecc4ba6b0763b934b6f2976e8c744088bb3c1d646fbb1ad0" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" @@ -1808,6 +2149,15 @@ version = "0.25.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f20c57d8d7db6d3b86154206ae5d8fba62dd39573114de97c2cb0578251f8e1" +[[package]] +name = "webpki-roots" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3de34ae270483955a94f4b21bdaaeb83d508bb84a01435f393818edb0012009" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1839,6 +2189,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e686886bc078bc1b0b600cac0147aadb815089b6e4da64016cbd754b6342700f" +dependencies = [ + "windows-targets 0.48.5", +] + [[package]] name = "windows-core" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index d1ac582..4e02675 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,5 @@ tokio = { version = "1.36", features = ["macros", "rt-multi-thread"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" confy = "0.6.0" +lavalink-rs = "0.10" +chrono = "0.4" diff --git a/src/commands/info.rs b/src/commands/info.rs new file mode 100644 index 0000000..c7030ec --- /dev/null +++ b/src/commands/info.rs @@ -0,0 +1,21 @@ +use chrono::Local; +use serenity::all::{CommandInteraction, Context}; +use serenity::builder::{CreateCommand, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter}; +use serenity::model::application::ResolvedOption; + +pub fn run(ctx: &Context, command: &CommandInteraction, _options: &[ResolvedOption]) -> CreateEmbed { + let username = command.user.name.as_str(); + let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S"); + CreateEmbed::new() + .author(CreateEmbedAuthor::new("Rustendo")) + .description("Botendo v7\ndeveloped by [moonleay](https://moonleay.net)\n\nCheck out the repository: https://git.moonleay.net/DiscordBots/Rustendo") + .footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username))) +} + + +pub fn register() -> CreateCommand { + CreateCommand::new("info").description("Infos about the bot") +} + +// >18/02/2024 @ 19:01:59 - bartlo +// >2024-02-19 17:58:39 | moonleay \ No newline at end of file diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 924dabf..6eaf7b9 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1 +1,3 @@ -pub mod ping; \ No newline at end of file +pub mod info; +pub mod play; +pub mod stop; \ No newline at end of file diff --git a/src/commands/ping.rs b/src/commands/ping.rs deleted file mode 100644 index f00a351..0000000 --- a/src/commands/ping.rs +++ /dev/null @@ -1,10 +0,0 @@ -use serenity::builder::CreateCommand; -use serenity::model::application::ResolvedOption; - -pub fn run(_options: &[ResolvedOption]) -> String { - "Hey, I'm alive!".to_string() -} - -pub fn register() -> CreateCommand { - CreateCommand::new("ping").description("A ping command") -} \ No newline at end of file diff --git a/src/commands/play.rs b/src/commands/play.rs new file mode 100644 index 0000000..b5d367c --- /dev/null +++ b/src/commands/play.rs @@ -0,0 +1,38 @@ + +use chrono::Local; +use serenity::all::{CommandInteraction, Context, ResolvedOption, ResolvedValue}; +use serenity::builder::{CreateCommand, CreateCommandOption, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter}; +use serenity::model::application::CommandOptionType; + +pub fn run(ctx: &Context, command: &CommandInteraction, options: &[ResolvedOption]) -> CreateEmbed { + let username = command.user.name.as_str(); + let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S"); + + let query = if let Some(ResolvedOption { + value: ResolvedValue::String(query), .. + }) = options.first() + { + query + } else { + return CreateEmbed::new() + .author(CreateEmbedAuthor::new("Rustendo")) + .title("Error 400") + .description("There is no query provied.") + .footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username))) + }; + + CreateEmbed::new() + .author(CreateEmbedAuthor::new("Rustendo")) + .title(format!("Searching for {}", query)) + .footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username))) +} + + +pub fn register() -> CreateCommand { + CreateCommand::new("play") + .description("Play music") + .add_option( + CreateCommandOption::new(CommandOptionType::String, "query", "Link or search term") + .required(true) + ) +} \ No newline at end of file diff --git a/src/commands/stop.rs b/src/commands/stop.rs new file mode 100644 index 0000000..0c4b517 --- /dev/null +++ b/src/commands/stop.rs @@ -0,0 +1,21 @@ +use chrono::Local; +use serenity::all::{CommandInteraction, Context}; +use serenity::builder::{CreateCommand, CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter}; +use serenity::model::application::ResolvedOption; + +pub fn run(ctx: &Context, command: &CommandInteraction, _options: &[ResolvedOption]) -> CreateEmbed { + let username = command.user.name.as_str(); + let current_time = Local::now().format("%Y-%m-%d @ %H:%M:%S"); + CreateEmbed::new() + .author(CreateEmbedAuthor::new("Rustendo")) + .title("I stopped and left\nJust like your girlfriend.") + .footer(CreateEmbedFooter::new(format!(">{} | {}", current_time, username))) +} + + +pub fn register() -> CreateCommand { + CreateCommand::new("stop").description("Stop playing and start leavin'") +} + +// >18/02/2024 @ 19:01:59 - bartlo +// >2024-02-19 17:58:39 | moonleay \ No newline at end of file diff --git a/src/handler/lavalink_event_handler.rs b/src/handler/lavalink_event_handler.rs new file mode 100644 index 0000000..a5e8aba --- /dev/null +++ b/src/handler/lavalink_event_handler.rs @@ -0,0 +1,8 @@ +use lavalink_rs::{client::LavalinkClient, model::events}; +use serenity::all::standard::macros::hook; + +#[hook] +pub async fn ready_event(client: LavalinkClient, _session_id: String, event: &events::Ready) { + client.delete_all_player_contexts().await.unwrap(); + println!("Lavalink is ready for Freddy:: {:?}", event); +} diff --git a/src/handler/mod.rs b/src/handler/mod.rs new file mode 100644 index 0000000..5610ef2 --- /dev/null +++ b/src/handler/mod.rs @@ -0,0 +1 @@ +pub mod lavalink_event_handler; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 137a653..866ecda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,30 +1,47 @@ mod commands; mod util; +mod handler; +use std::error::Error; + +use lavalink_rs::client::LavalinkClient; +use lavalink_rs::model::events::Events; +use lavalink_rs::model::UserId; +use lavalink_rs::node::NodeBuilder; use serenity::all::OnlineStatus; use serenity::async_trait; -use serenity::builder::{CreateInteractionResponse, CreateInteractionResponseMessage}; +use serenity::builder::{CreateEmbed, CreateEmbedAuthor, CreateEmbedFooter, CreateInteractionResponse, CreateInteractionResponseMessage}; use serenity::gateway::ActivityData; use serenity::model::application::{Command, Interaction}; use serenity::model::gateway::Ready; use serenity::prelude::*; use util::config; +use crate::handler::lavalink_event_handler; + + struct Handler; +pub struct Data { + pub lavalink: LavalinkClient, +} + #[async_trait] impl EventHandler for Handler { async fn interaction_create(&self, ctx: Context, interaction: Interaction) { if let Interaction::Command(command) = interaction { - println!("Received command interaction: {command:#?}"); - let content = match command.data.name.as_str() { - "ping" => Some(commands::ping::run(&command.data.options())), - _ => Some("not implemented :(".to_string()), + "info" => Some(commands::info::run(&ctx, &command, &command.data.options())), + "play" => Some(commands::play::run(&ctx, &command, &command.data.options())), + "stop" => Some(commands::stop::run(&ctx, &command, &command.data.options())), + _ => Some(CreateEmbed::new() + .author(CreateEmbedAuthor::new("Rustendo")) + .description("Cannot find command") + .footer(CreateEmbedFooter::new("Error during execution"))), }; - if let Some(content) = content { - let data = CreateInteractionResponseMessage::new().content(content); + if let Some(embed) = content { + let data = CreateInteractionResponseMessage::new().embed(embed); let builder = CreateInteractionResponse::Message(data); if let Err(why) = command.create_response(&ctx.http, builder).await { println!("Cannot respond to slash command: {why}"); @@ -36,9 +53,9 @@ impl EventHandler for Handler { async fn ready(&self, ctx: Context, ready: Ready) { println!("{} is connected!", ready.user.name); - let _guild_command = - Command::create_global_command(&ctx.http, commands::ping::register()) - .await; + let _command = Command::create_global_command(&ctx.http, commands::info::register()).await; + let _command = Command::create_global_command(&ctx.http, commands::stop::register()).await; + let _command = Command::create_global_command(&ctx.http, commands::play::register()).await; println!("Created all public / commands"); } @@ -55,12 +72,32 @@ async fn main() { "); - + // Load config let config = config::load().unwrap(); - + // Set status let status = OnlineStatus::DoNotDisturb; let activity = ActivityData::streaming("music", "https://twitch.tv/moonleaytv").unwrap(); - // Build our client. + + let node_builder = NodeBuilder { + hostname: config.lavalink_address, + password: config.lavalink_password, + user_id: UserId(config.user_id), + ..Default::default() + }; + + // let event = Events::default(); + let event = Events { + ready: Some(lavalink_event_handler::ready_event), + ..Default::default() + }; + + // Connect to lavalink + let lavalink_client = LavalinkClient::new(event, vec![node_builder]); + tokio::spawn(async move { + lavalink_client.start().await; + }); + + // Build the client let mut client = Client::builder(config.discord_token, GatewayIntents::empty()) .event_handler(Handler) .status(status) diff --git a/src/util/config.rs b/src/util/config.rs index 89d5730..24b216b 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -5,7 +5,10 @@ use std::error::Error; #[derive(Deserialize, Serialize)] pub struct Config { - pub discord_token: String + pub discord_token: String, + pub lavalink_address: String, + pub lavalink_password: String, + pub user_id: u64 } const CONFIG_FILE: &str = "./data/config.json"; @@ -22,7 +25,10 @@ pub fn load() -> Result> { fn create_empty() -> fs::File{ let example_config = Config { - discord_token: "paste_your_config".to_string() + discord_token: "paste_your_token".to_string(), + lavalink_address: "paste_your_lavalink_address".to_string(), + lavalink_password: "paste_your_lavalink_password".to_string(), + user_id: 1 }; let mut config_file = fs::File::create(CONFIG_FILE).unwrap(); diff --git a/src/util/mod.rs b/src/util/mod.rs index ef68c36..a105933 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1 +1 @@ -pub mod config; +pub mod config; \ No newline at end of file