cache join sounds

This commit is contained in:
jellywx 2021-04-15 11:40:41 +01:00
parent 900fe6a4f0
commit f237ad413d
4 changed files with 240 additions and 212 deletions

227
Cargo.lock generated
View File

@ -49,31 +49,15 @@ checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "async-trait"
version = "0.1.48"
version = "0.1.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36ea56748e10732c49404c153638a15ec3d6211ec5ff35d9bb20e13b93576adf"
checksum = "589652ce7ccb335d1e7ecb3be145425702b290dbcb7029bbeaae263fc1d87b48"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "async-tungstenite"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7cc5408453d37e2b1c6f01d8078af1da58b6cfa6a80fa2ede3bd2b9a6ada9c4"
dependencies = [
"futures-io",
"futures-util",
"log 0.4.14",
"pin-project",
"tokio",
"tokio-rustls",
"tungstenite 0.11.1",
"webpki-roots 0.20.0",
]
[[package]]
name = "async-tungstenite"
version = "0.13.1"
@ -86,8 +70,8 @@ dependencies = [
"pin-project-lite",
"tokio",
"tokio-rustls",
"tungstenite 0.13.0",
"webpki-roots 0.21.1",
"tungstenite",
"webpki-roots",
]
[[package]]
@ -141,12 +125,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base64"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
[[package]]
name = "base64"
version = "0.13.0"
@ -209,12 +187,6 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "bytes"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38"
[[package]]
name = "bytes"
version = "1.0.1"
@ -265,8 +237,7 @@ dependencies = [
[[package]]
name = "command_attr"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fe1f0b69fde68f40ea2ee6ca8db23bc40d2e593db884659a65d8486032cc65b"
source = "git+https://github.com/serenity-rs/serenity?branch=next#ed8d7ab1df6427be825b66c590fd02dd3ab5dfc0"
dependencies = [
"proc-macro2",
"quote",
@ -312,9 +283,9 @@ dependencies = [
[[package]]
name = "crossbeam-channel"
version = "0.5.0"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dca26ee1f8d361640700bde38b2c37d8c22b3ce2d360e1fc1c74ea4b0aa7d775"
checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
dependencies = [
"cfg-if 1.0.0",
"crossbeam-utils 0.8.3",
@ -372,9 +343,9 @@ dependencies = [
[[package]]
name = "discortp"
version = "0.2.2"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c0d482488c336a2164529765da3f645f26215df9c2033137ddedac333c8e2e8"
checksum = "b8a712fe4034500af7e5063aa55a272354a36472120c8511c358fbd4f0230bec"
dependencies = [
"glob",
"pnet_macros",
@ -439,9 +410,9 @@ dependencies = [
[[package]]
name = "flume"
version = "0.10.2"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "531a685ab99b8f60a271b44d5dd1a76e55124a8c9fa0407b7a8e9cd172d5b588"
checksum = "11fce69af4d4582ea989e6adfc5c9b81fd2071ff89234e5c14675c82a85217df"
dependencies = [
"futures-core",
"futures-sink",
@ -489,9 +460,9 @@ checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]]
name = "futures"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1"
checksum = "a9d5813545e459ad3ca1bff9915e9ad7f1a47dc6a91b627ce321d5863b7dd253"
dependencies = [
"futures-channel",
"futures-core",
@ -504,9 +475,9 @@ dependencies = [
[[package]]
name = "futures-channel"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939"
checksum = "ce79c6a52a299137a6013061e0cf0e688fce5d7f1bc60125f520912fdb29ec25"
dependencies = [
"futures-core",
"futures-sink",
@ -514,15 +485,15 @@ dependencies = [
[[package]]
name = "futures-core"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94"
checksum = "098cd1c6dda6ca01650f1a37a794245eb73181d0d4d4e955e2f3c37db7af1815"
[[package]]
name = "futures-executor"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891a4b7b96d84d5940084b2a37632dd65deeae662c114ceaa2c879629c9c0ad1"
checksum = "10f6cb7042eda00f0049b1d2080aa4b93442997ee507eb3828e8bd7577f94c9d"
dependencies = [
"futures-core",
"futures-task",
@ -531,15 +502,15 @@ dependencies = [
[[package]]
name = "futures-io"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59"
checksum = "365a1a1fb30ea1c03a830fdb2158f5236833ac81fa0ad12fe35b29cddc35cb04"
[[package]]
name = "futures-macro"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea405816a5139fb39af82c2beb921d52143f556038378d6db21183a5c37fbfb7"
checksum = "668c6733a182cd7deb4f1de7ba3bf2120823835b3bcfbeacf7d2c4a773c1bb8b"
dependencies = [
"proc-macro-hack",
"proc-macro2",
@ -549,21 +520,21 @@ dependencies = [
[[package]]
name = "futures-sink"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3"
checksum = "5c5629433c555de3d82861a7a4e3794a4c40040390907cfbfd7143a92a426c23"
[[package]]
name = "futures-task"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80"
checksum = "ba7aa51095076f3ba6d9a1f702f74bd05ec65f555d70d2033d55ba8d69f581bc"
[[package]]
name = "futures-util"
version = "0.3.13"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1"
checksum = "3c144ad54d60f23927f0a6b6d816e4271278b64f005ad65e4e35291d2de9c025"
dependencies = [
"futures-channel",
"futures-core",
@ -638,7 +609,7 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc018e188373e2777d0ef2467ebff62a08e66c3f5857b23c8fbec3018210dc00"
dependencies = [
"bytes 1.0.1",
"bytes",
"fnv",
"futures-core",
"futures-sink",
@ -699,7 +670,7 @@ version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527e8c9ac747e28542699a951517aa9a6945af506cd1f2e1b53a576c17b6cc11"
dependencies = [
"bytes 1.0.1",
"bytes",
"fnv",
"itoa",
]
@ -710,7 +681,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dfb77c123b4e2f72a2069aeae0b4b4949cc7e966df277813fc16347e7549737"
dependencies = [
"bytes 1.0.1",
"bytes",
"http",
"pin-project-lite",
]
@ -739,7 +710,7 @@ version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bf09f61b52cfcf4c00de50df88ae423d6c02354e385a86341133b5338630ad1"
dependencies = [
"bytes 1.0.1",
"bytes",
"futures-channel",
"futures-core",
"futures-util",
@ -778,7 +749,7 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905"
dependencies = [
"bytes 1.0.1",
"bytes",
"hyper",
"native-tls",
"tokio",
@ -806,22 +777,13 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "input_buffer"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19a8a95243d5a0398cae618ec29477c6e3cb631152be5c19481f80bc71559754"
dependencies = [
"bytes 0.5.6",
]
[[package]]
name = "input_buffer"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413"
dependencies = [
"bytes 1.0.1",
"bytes",
]
[[package]]
@ -1219,7 +1181,7 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd56cbd21fea48d0c440b41cd69c589faacade08c992d9a54e471b79d0fd13eb"
dependencies = [
"base64 0.13.0",
"base64",
"once_cell",
"regex",
]
@ -1270,15 +1232,15 @@ checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c"
[[package]]
name = "pnet_base"
version = "0.26.0"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7cd5f7e15220afa66b0a9a62841ea10089f39dcaa1c29752c0b22dfc03111b5"
checksum = "4e4688aa497ef62129f302a5800ebde67825f8ff129f43690ca84099f6620bed"
[[package]]
name = "pnet_macros"
version = "0.26.0"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbbd5c52c6e04aa720400f9c71cd0e8bcb38cd13421d5caabd9035e9efa47de9"
checksum = "d894a90dbdbe976e624453fc31b1912f658083778329442dda1cca94f76a3e76"
dependencies = [
"regex",
"syntex",
@ -1287,9 +1249,9 @@ dependencies = [
[[package]]
name = "pnet_macros_support"
version = "0.26.0"
version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf9c5c0c36766d0a4da9ab268c0700771b8ec367b9463fd678109fa28463c5b"
checksum = "4b99269a458570bc06a9132254349f6543d9abc92e88b68d8de934aac9481f6c"
dependencies = [
"pnet_base",
]
@ -1429,9 +1391,9 @@ dependencies = [
[[package]]
name = "redox_syscall"
version = "0.2.5"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94341e4e44e24f6b591b59e47a8a027df12e008d73fd5672dbea9cc22f4507d9"
checksum = "8270314b5ccceb518e7e578952f0b72b88222d02e8f77f5ecf7abbb673539041"
dependencies = [
"bitflags 1.2.1",
]
@ -1473,12 +1435,12 @@ dependencies = [
[[package]]
name = "reqwest"
version = "0.11.2"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf12057f289428dbf5c591c74bf10392e4a8003f993405a902f20117019022d4"
checksum = "2296f2fac53979e8ccbc4a1136b25dcefd37be9ed7e4a1f6b05a6029c84ff124"
dependencies = [
"base64 0.13.0",
"bytes 1.0.1",
"base64",
"bytes",
"encoding_rs",
"futures-core",
"futures-util",
@ -1507,7 +1469,7 @@ dependencies = [
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"webpki-roots 0.21.1",
"webpki-roots",
"winreg",
]
@ -1560,7 +1522,7 @@ version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b"
dependencies = [
"base64 0.13.0",
"base64",
"log 0.4.14",
"ring",
"sct",
@ -1613,9 +1575,9 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sct"
version = "0.6.0"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3042af939fca8c3453b7af0f1c66e533a15a86169e39de2657310ade8f98d3c"
checksum = "b362b83898e0e69f38515b82ee15aa80636befe47c3b6d3d89a911e78fc228ce"
dependencies = [
"ring",
"untrusted",
@ -1701,14 +1663,13 @@ dependencies = [
[[package]]
name = "serenity"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "deead3f7ecbbbe4c249e07af17686937ccb9d7fa24ca3accd1d223e369a75272"
source = "git+https://github.com/serenity-rs/serenity?branch=next#ed8d7ab1df6427be825b66c590fd02dd3ab5dfc0"
dependencies = [
"async-trait",
"async-tungstenite 0.11.0",
"base64 0.13.0",
"async-tungstenite",
"base64",
"bitflags 1.2.1",
"bytes 1.0.1",
"bytes",
"chrono",
"command_attr",
"flate2",
@ -1728,8 +1689,7 @@ dependencies = [
[[package]]
name = "serenity-voice-model"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "158aeb823791f79bbb92110212970797757fee7102784453dcb9b172a8af272b"
source = "git+https://github.com/serenity-rs/serenity?branch=next#ed8d7ab1df6427be825b66c590fd02dd3ab5dfc0"
dependencies = [
"bitflags 1.2.1",
"enum_primitive",
@ -1808,12 +1768,11 @@ dependencies = [
[[package]]
name = "songbird"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "358efdd4b111021b0b7499fa087aa3cba8f01ae7210d37ab45620e4791c09ab8"
version = "0.1.5"
source = "git+https://github.com/serenity-rs/songbird?branch=next#743845e40813edfbcb86f2b83e43ef9c907e860a"
dependencies = [
"async-trait",
"async-tungstenite 0.13.1",
"async-tungstenite",
"audiopus",
"byteorder",
"dashmap",
@ -1821,13 +1780,13 @@ dependencies = [
"flume",
"futures",
"parking_lot",
"pin-project",
"rand 0.8.3",
"serde",
"serde_json",
"serenity",
"serenity-voice-model",
"spin_sleep",
"spinning_top",
"streamcatcher",
"tokio",
"tracing",
@ -1840,7 +1799,7 @@ dependencies = [
[[package]]
name = "soundfx-rs"
version = "1.2.2"
version = "1.2.4"
dependencies = [
"dashmap",
"dotenv",
@ -1875,9 +1834,9 @@ dependencies = [
[[package]]
name = "spinning_top"
version = "0.2.2"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e529d73e80d64b5f2631f9035113347c578a1c9c7774b83a2b880788459ab36"
checksum = "8bd0ab6b8c375d2d963503b90d3770010d95bc3b5f98036f948dee24bf4e8879"
dependencies = [
"lock_api",
]
@ -1913,11 +1872,11 @@ checksum = "b1cad9cae4ca8947eba1a90e8ec7d3c59e7a768e2f120dc9013b669c34a90711"
dependencies = [
"ahash 0.6.3",
"atoi",
"base64 0.13.0",
"base64",
"bigdecimal",
"bitflags 1.2.1",
"byteorder",
"bytes 1.0.1",
"bytes",
"crossbeam-channel",
"crossbeam-queue",
"crossbeam-utils 0.8.3",
@ -1950,7 +1909,7 @@ dependencies = [
"tokio-stream",
"url",
"webpki",
"webpki-roots 0.21.1",
"webpki-roots",
"whoami",
]
@ -2174,12 +2133,12 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
[[package]]
name = "tokio"
version = "1.4.0"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "134af885d758d645f0f0505c9a8b3f9bf8a348fd822e112ab5248138348f1722"
checksum = "83f0c8e7c0addab50b663055baf787d0af7f413a46e6e7fb9559a4e4db7137a5"
dependencies = [
"autocfg 1.0.1",
"bytes 1.0.1",
"bytes",
"libc",
"memchr",
"mio",
@ -2236,11 +2195,11 @@ dependencies = [
[[package]]
name = "tokio-util"
version = "0.6.5"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5143d049e85af7fbc36f5454d990e62c2df705b3589f123b71f441b6b59f443f"
checksum = "940a12c99365c31ea8dd9ba04ec1be183ffe4920102bb7122c2f515437601e8e"
dependencies = [
"bytes 1.0.1",
"bytes",
"futures-core",
"futures-sink",
"log 0.4.14",
@ -2303,37 +2262,18 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "tungstenite"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [
"base64 0.12.3",
"byteorder",
"bytes 0.5.6",
"http",
"httparse",
"input_buffer 0.3.1",
"log 0.4.14",
"rand 0.7.3",
"sha-1",
"url",
"utf-8",
]
[[package]]
name = "tungstenite"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fe8dada8c1a3aeca77d6b51a4f1314e0f4b8e438b7b1b71e3ddaca8080e4093"
dependencies = [
"base64 0.13.0",
"base64",
"byteorder",
"bytes 1.0.1",
"bytes",
"http",
"httparse",
"input_buffer 0.4.0",
"input_buffer",
"log 0.4.14",
"rand 0.8.3",
"rustls",
@ -2342,14 +2282,14 @@ dependencies = [
"url",
"utf-8",
"webpki",
"webpki-roots 0.21.1",
"webpki-roots",
]
[[package]]
name = "typemap_rev"
version = "0.1.4"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335fb14412163adc9ed4a3e53335afaa7a4b72bdd122e5f72f51b8f1db1a131e"
checksum = "ed5b74f0a24b5454580a79abb6994393b09adf0ab8070f15827cb666255de155"
[[package]]
name = "typenum"
@ -2579,15 +2519,6 @@ dependencies = [
"untrusted",
]
[[package]]
name = "webpki-roots"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f20dea7535251981a9670857150d571846545088359b28e4951d350bdaf179f"
dependencies = [
"webpki",
]
[[package]]
name = "webpki-roots"
version = "0.21.1"

View File

@ -1,18 +1,18 @@
[package]
name = "soundfx-rs"
version = "1.2.2"
version = "1.2.4"
authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"
[dependencies]
serenity = { version = "0.10", features = ["voice", "collector"] }
songbird = { git = "https://github.com/serenity-rs/songbird", branch = "next" }
serenity = { git = "https://github.com/serenity-rs/serenity", branch = "next", features = ["voice", "collector"] }
sqlx = { version = "0.5", default-features = false, features = ["runtime-tokio-rustls", "macros", "mysql", "bigdecimal"] }
dotenv = "0.15"
tokio = { version = "1.0", features = ["fs", "process", "io-util"] }
tokio = { version = "1", features = ["fs", "process", "io-util"] }
lazy_static = "1.4"
reqwest = "0.11"
env_logger = "0.8"
songbird = "0.1.6"
regex = "1.4"
log = "0.4"
serde_json = "1.0"

View File

@ -16,6 +16,8 @@ use crate::{
sound::Sound,
};
use log::info;
use regex_command_attr::command;
use serenity::{
@ -46,7 +48,9 @@ use sqlx::mysql::MySqlPool;
use dotenv::dotenv;
use crate::sound::JoinSoundCtx;
use dashmap::DashMap;
use serenity::model::id::UserId;
use std::{collections::HashMap, convert::TryFrom, env, sync::Arc, time::Duration};
use tokio::sync::{MutexGuard, RwLock};
@ -74,6 +78,12 @@ impl TypeMapKey for GuildDataCache {
type Value = Arc<DashMap<GuildId, Arc<RwLock<GuildData>>>>;
}
struct JoinSoundCache;
impl TypeMapKey for JoinSoundCache {
type Value = Arc<DashMap<UserId, Option<u32>>>;
}
const THEME_COLOR: u32 = 0x00e0f3;
lazy_static! {
@ -178,20 +188,7 @@ impl EventHandler for Handler {
}
if allowed_greets {
let join_id_res = sqlx::query!(
"
SELECT join_sound_id
FROM users
WHERE user = ? AND join_sound_id IS NOT NULL
",
new.user_id.as_u64()
)
.fetch_one(&pool)
.await;
if let Ok(join_id_record) = join_id_res {
let join_id = join_id_record.join_sound_id;
if let Some(join_id) = ctx.join_sound(new.user_id).await {
let mut sound = sqlx::query_as_unchecked!(
Sound,
"
@ -371,9 +368,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.unwrap();
let guild_data_cache = Arc::new(DashMap::new());
let join_sound_cache = Arc::new(DashMap::new());
let mut data = client.data.write().await;
data.insert::<GuildDataCache>(guild_data_cache);
data.insert::<JoinSoundCache>(join_sound_cache);
data.insert::<MySQL>(mysql_pool);
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));
@ -383,7 +382,45 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
}
}
client.start_autosharded().await?;
if let Ok((Some(lower), Some(upper))) = env::var("SHARD_RANGE").map(|sr| {
let mut split = sr
.split(',')
.map(|val| val.parse::<u64>().expect("SHARD_RANGE not an integer"));
(split.next(), split.next())
}) {
let total_shards = env::var("SHARD_COUNT")
.map(|shard_count| shard_count.parse::<u64>().ok())
.ok()
.flatten()
.expect("No SHARD_COUNT provided, but SHARD_RANGE was provided");
assert!(
lower < upper,
"SHARD_RANGE lower limit is not less than the upper limit"
);
info!(
"Starting client fragment with shards {}-{}/{}",
lower, upper, total_shards
);
client
.start_shard_range([lower, upper], total_shards)
.await?;
} else if let Ok(total_shards) = env::var("SHARD_COUNT").map(|shard_count| {
shard_count
.parse::<u64>()
.expect("SHARD_COUNT not an integer")
}) {
info!("Starting client with {} shards", total_shards);
client.start_shards(total_shards).await?;
} else {
info!("Starting client as autosharded");
client.start_autosharded().await?;
}
Ok(())
}
@ -1332,29 +1369,8 @@ async fn set_greet_sound(ctx: &Context, msg: &Message, args: Args) -> CommandRes
let query = args.rest();
let user_id = *msg.author.id.as_u64();
let _ = sqlx::query!(
"
INSERT IGNORE INTO users (user)
VALUES (?)
",
user_id
)
.execute(&pool)
.await;
if query.len() == 0 {
sqlx::query!(
"
UPDATE users
SET
join_sound_id = NULL
WHERE
user = ?
",
user_id
)
.execute(&pool)
.await?;
ctx.update_join_sound(user_id, None).await;
msg.channel_id
.say(&ctx, "Your greet sound has been unset.")
@ -1371,7 +1387,7 @@ WHERE
match sound_vec.first() {
Some(sound) => {
sound.set_as_greet(user_id, pool).await?;
ctx.update_join_sound(user_id, Some(sound.id)).await;
msg.channel_id
.say(

View File

@ -8,6 +8,109 @@ use songbird::input::restartable::Restartable;
use std::{env, path::Path};
use crate::{JoinSoundCache, MySQL};
use serenity::{async_trait, model::id::UserId, prelude::Context};
#[async_trait]
pub trait JoinSoundCtx {
async fn join_sound<U: Into<UserId> + Send + Sync>(&self, user_id: U) -> Option<u32>;
async fn update_join_sound<U: Into<UserId> + Send + Sync>(
&self,
user_id: U,
join_id: Option<u32>,
);
}
#[async_trait]
impl JoinSoundCtx for Context {
async fn join_sound<U: Into<UserId> + Send + Sync>(&self, user_id: U) -> Option<u32> {
let user_id = user_id.into();
let join_sound_cache = self
.data
.read()
.await
.get::<JoinSoundCache>()
.cloned()
.unwrap();
let x = if let Some(join_sound_id) = join_sound_cache.get(&user_id) {
join_sound_id.value().clone()
} else {
let join_sound_id = {
let pool = self.data.read().await.get::<MySQL>().cloned().unwrap();
let join_id_res = sqlx::query!(
"
SELECT join_sound_id
FROM users
WHERE user = ?
",
user_id.as_u64()
)
.fetch_one(&pool)
.await;
if let Ok(row) = join_id_res {
row.join_sound_id
} else {
None
}
};
join_sound_cache.insert(user_id, join_sound_id);
join_sound_id
};
x
}
async fn update_join_sound<U: Into<UserId> + Send + Sync>(
&self,
user_id: U,
join_id: Option<u32>,
) {
let user_id = user_id.into();
let join_sound_cache = self
.data
.read()
.await
.get::<JoinSoundCache>()
.cloned()
.unwrap();
join_sound_cache.insert(user_id, join_id);
let pool = self.data.read().await.get::<MySQL>().cloned().unwrap();
if join_sound_cache.get(&user_id).is_none() {
let _ = sqlx::query!(
"
INSERT IGNORE INTO users (user)
VALUES (?)
",
user_id.as_u64()
)
.execute(&pool)
.await;
}
let _ = sqlx::query!(
"
UPDATE users
SET
join_sound_id = ?
WHERE
user = ?
",
join_id,
user_id.as_u64()
)
.execute(&pool)
.await;
}
}
pub struct Sound {
pub name: String,
pub id: u32,
@ -195,28 +298,6 @@ SELECT COUNT(1) as count
Ok(c as u32)
}
pub async fn set_as_greet(
&self,
user_id: u64,
db_pool: MySqlPool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
sqlx::query!(
"
UPDATE users
SET
join_sound_id = ?
WHERE
user = ?
",
self.id,
user_id
)
.execute(&db_pool)
.await?;
Ok(())
}
pub async fn commit(
&self,
db_pool: MySqlPool,