working on del command

This commit is contained in:
jude 2020-09-08 00:12:17 +01:00
parent f654db76f7
commit f0a1bb3a71
4 changed files with 194 additions and 81 deletions

157
Cargo.lock generated
View File

@ -56,14 +56,14 @@ dependencies = [
[[package]]
name = "async-tls"
version = "0.6.0"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce6977f57fa68da77ffe5542950d47e9c23d65f5bc7cb0a9f8700996913eec7"
checksum = "d7e7fbc0843fc5ad3d5ca889c5b2bea9130984d34cd0e62db57ab70c2529a8e3"
dependencies = [
"futures",
"rustls 0.16.0",
"rustls",
"webpki",
"webpki-roots 0.17.0",
"webpki-roots 0.20.0",
]
[[package]]
@ -79,9 +79,9 @@ dependencies = [
[[package]]
name = "async-tungstenite"
version = "0.4.2"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4187bb446c8ecb8849f17cef7553db8bdb09e482e806257130189958fb42dca7"
checksum = "a5c45a0dd44b7e6533ac4e7acc38ead1a3b39885f5bbb738140d30ea528abc7c"
dependencies = [
"async-tls",
"futures-io",
@ -98,21 +98,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "base64"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
dependencies = [
"byteorder",
]
[[package]]
name = "base64"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
[[package]]
name = "base64"
version = "0.12.3"
@ -145,7 +130,16 @@ dependencies = [
"block-padding",
"byte-tools",
"byteorder",
"generic-array",
"generic-array 0.12.3",
]
[[package]]
name = "block-buffer"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
@ -217,9 +211,9 @@ dependencies = [
[[package]]
name = "command_attr"
version = "0.3.0-rc.0"
version = "0.3.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "093ee06b40fcba41b22ddf85de7ed33728fe2e246fcdbc1b1d3ab26f87fabf3b"
checksum = "c538daab2daaf13de61cea91648a62bb11d267ef629f707d5fe3dd080043ab4d"
dependencies = [
"proc-macro2",
"quote",
@ -242,6 +236,12 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3a71ab494c0b5b860bdc8407ae08978052417070c2ced38573a9157ad75b8ac"
[[package]]
name = "cpuid-bool"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634"
[[package]]
name = "crc32fast"
version = "1.2.0"
@ -279,7 +279,16 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
dependencies = [
"generic-array",
"generic-array 0.12.3",
]
[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066"
dependencies = [
"generic-array 0.14.4",
]
[[package]]
@ -462,6 +471,16 @@ dependencies = [
"typenum",
]
[[package]]
name = "generic-array"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.1.14"
@ -586,7 +605,7 @@ dependencies = [
"futures-util",
"hyper",
"log",
"rustls 0.18.0",
"rustls",
"tokio",
"tokio-rustls",
"webpki",
@ -890,6 +909,12 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c"
[[package]]
name = "opaque-debug"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
[[package]]
name = "openssl"
version = "0.10.30"
@ -1119,7 +1144,7 @@ version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12427a5577082c24419c9c417db35cfeb65962efc7675bb6b0d5f1f9d315bfe6"
dependencies = [
"base64 0.12.3",
"base64",
"bytes",
"encoding_rs",
"futures-core",
@ -1138,7 +1163,7 @@ dependencies = [
"native-tls",
"percent-encoding",
"pin-project-lite",
"rustls 0.18.0",
"rustls",
"serde",
"serde_json",
"serde_urlencoded",
@ -1168,26 +1193,13 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "rustls"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b25a18b1bf7387f0145e7f8324e700805aade3842dd3db2e74e4cdeb4677c09e"
dependencies = [
"base64 0.10.1",
"log",
"ring",
"sct",
"webpki",
]
[[package]]
name = "rustls"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cac94b333ee2aac3284c5b8a1b7fb4dd11cba88c244e3fe33cdbd047af0eb693"
dependencies = [
"base64 0.12.3",
"base64",
"log",
"ring",
"sct",
@ -1288,14 +1300,13 @@ dependencies = [
[[package]]
name = "serenity"
version = "0.9.0-rc.0"
version = "0.9.0-rc.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f2b6557e836289a35b45ecc9da78b464de79cec3aa9b7cfd07f654cbc478c00"
checksum = "21e935a7f3f4752257183ee1f3553b10ea5b514a55de0e536dca7f3742b97d18"
dependencies = [
"async-tls",
"async-trait",
"async-tungstenite",
"base64 0.12.3",
"base64",
"bitflags",
"bytes",
"chrono",
@ -1304,7 +1315,6 @@ dependencies = [
"futures",
"log",
"reqwest",
"rustls 0.18.0",
"serde",
"serde_json",
"static_assertions",
@ -1312,8 +1322,6 @@ dependencies = [
"typemap_rev",
"url",
"uwl",
"webpki",
"webpki-roots 0.19.0",
]
[[package]]
@ -1322,10 +1330,23 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df"
dependencies = [
"block-buffer",
"digest",
"block-buffer 0.7.3",
"digest 0.8.1",
"fake-simd",
"opaque-debug",
"opaque-debug 0.2.3",
]
[[package]]
name = "sha-1"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "170a36ea86c864a3f16dd2687712dd6646f7019f301e57537c7f4dc9f5916770"
dependencies = [
"block-buffer 0.9.0",
"cfg-if",
"cpuid-bool",
"digest 0.9.0",
"opaque-debug 0.3.0",
]
[[package]]
@ -1334,10 +1355,10 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69"
dependencies = [
"block-buffer",
"digest",
"block-buffer 0.7.3",
"digest 0.8.1",
"fake-simd",
"opaque-debug",
"opaque-debug 0.2.3",
]
[[package]]
@ -1403,18 +1424,18 @@ checksum = "88ac5a436f941c42eac509471a730df5c3c58e1450e68cd39afedbd948206273"
dependencies = [
"async-native-tls",
"async-stream",
"base64 0.12.3",
"base64",
"bigdecimal",
"bitflags",
"byteorder",
"chrono",
"crossbeam-queue",
"crossbeam-utils",
"digest",
"digest 0.8.1",
"futures-channel",
"futures-core",
"futures-util",
"generic-array",
"generic-array 0.12.3",
"hex",
"libc",
"log",
@ -1422,7 +1443,7 @@ dependencies = [
"num-bigint",
"percent-encoding",
"rand",
"sha-1",
"sha-1 0.8.2",
"sha2",
"sqlformat",
"tokio",
@ -1565,7 +1586,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "228139ddd4fea3fa345a29233009635235833e52807af7ea6448ead03890d6a9"
dependencies = [
"futures-core",
"rustls 0.18.0",
"rustls",
"tokio",
"webpki",
]
@ -1628,11 +1649,11 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "tungstenite"
version = "0.10.1"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfea31758bf674f990918962e8e5f07071a3161bd7c4138ed23e416e1ac4264e"
checksum = "f0308d80d86700c5878b9ef6321f020f29b1bb9d5ff3cab25e75e23f3a492a23"
dependencies = [
"base64 0.11.0",
"base64",
"byteorder",
"bytes",
"http",
@ -1640,7 +1661,7 @@ dependencies = [
"input_buffer",
"log",
"rand",
"sha-1",
"sha-1 0.9.1",
"url",
"utf-8",
]
@ -1843,18 +1864,18 @@ dependencies = [
[[package]]
name = "webpki-roots"
version = "0.17.0"
version = "0.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a262ae37dd9d60f60dd473d1158f9fbebf110ba7b6a5051c8160460f6043718b"
checksum = "f8eff4b7516a57307f9349c64bf34caa34b940b66fed4b2fb3136cb7386e5739"
dependencies = [
"webpki",
]
[[package]]
name = "webpki-roots"
version = "0.19.0"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8eff4b7516a57307f9349c64bf34caa34b940b66fed4b2fb3136cb7386e5739"
checksum = "0f20dea7535251981a9670857150d571846545088359b28e4951d350bdaf179f"
dependencies = [
"webpki",
]

View File

@ -5,7 +5,7 @@ authors = ["jellywx <judesouthworth@pm.me>"]
edition = "2018"
[dependencies]
serenity = "0.9.0-rc.0"
serenity = { version = "0.9.0-rc.1", features = ["collector"] }
dotenv = "0.15"
tokio = { version = "0.2.19", features = ["fs", "sync", "process", "io-util"] }
reqwest = "0.10.6"

View File

@ -231,6 +231,7 @@ impl LookFlags {
}
#[command]
#[permission_level(Managed)]
async fn look(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
@ -247,7 +248,7 @@ async fn look(ctx: &Context, msg: &Message, args: String) -> CommandResult {
sqlx::query_as!(Reminder,
"
SELECT
reminders.id, reminders.time, reminders.name
reminders.id, reminders.time, reminders.name, reminders.channel_id
FROM
reminders
INNER JOIN
@ -263,29 +264,23 @@ LIMIT
", guild_id, channel_id, enabled, flags.limit)
.fetch_all(&pool)
.await
.unwrap()
}
else {
sqlx::query_as!(Reminder,
"
SELECT
reminders.id, reminders.time, reminders.name
id, time, name, channel_id
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
WHERE
channels.channel = ? AND
reminders.channel_id = (SELECT id FROM channels WHERE channel = ?) AND
reminders.enabled != ?
LIMIT
?
", msg.channel_id.as_u64(), enabled, flags.limit)
.fetch_all(&pool)
.await
.unwrap()
};
}.unwrap();
if reminders.len() == 0 {
let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "look/no_reminders").await).await;
@ -303,3 +298,99 @@ LIMIT
Ok(())
}
#[command]
#[permission_level(Managed)]
async fn delete(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().cloned().expect("Could not get SQLPool from data");
let user_data = UserData::from_id(&msg.author, &ctx, &pool).await.unwrap();
let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "del/listing").await).await;
let reminders = if let Some(guild_id) = msg.guild_id.map(|f| f.as_u64().to_owned()) {
sqlx::query_as!(Reminder,
"
SELECT
reminders.id, reminders.time, reminders.name, reminders.channel_id
FROM
reminders
INNER JOIN
channels
ON
reminders.channel_id = channels.id
WHERE
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?)
", guild_id)
.fetch_all(&pool)
.await
}
else {
sqlx::query_as!(Reminder,
"
SELECT
id, time, name, channel_id
FROM
reminders
WHERE
channel_id = (SELECT id FROM channels WHERE channel = ?)
", msg.channel_id.as_u64())
.fetch_all(&pool)
.await
}.unwrap();
let mut reminder_ids: Vec<u32> = vec![];
let enumerated_reminders = reminders
.iter()
.enumerate()
.map(|(count, reminder)| {
reminder_ids.push(reminder.id);
format!("**{}**: '{}' *{}* at {}", count + 1, reminder.name, reminder.channel_id, reminder.time)
})
.collect::<Vec<String>>()
.join("\n");
let _ = msg.channel_id.say(&ctx, enumerated_reminders).await;
let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "del/listed").await).await;
let reply = msg.channel_id.await_reply(&ctx)
.filter_limit(1)
.author_id(msg.author.id)
.channel_id(msg.channel_id).await;
if let Some(content) = reply.map(|m| m.content.replace(",", " ")) {
let parts = content.split(" ").filter(|i| i.len() > 0).collect::<Vec<&str>>();
let valid_parts = parts
.iter()
.filter_map(
|i|
i.parse::<usize>()
.ok()
.map(
|val|
reminder_ids.get(val)
)
.flatten()
)
.map(|item| item.to_string())
.collect::<Vec<String>>();
if parts.len() == valid_parts.len() {
sqlx::query!(
"
DELETE FROM reminders WHERE id IN (?)
", valid_parts.join(","))
.execute(&pool)
.await;
// TODO add deletion events to event list
let _ = msg.channel_id.say(&ctx, user_data.response(&pool, "del/count").await).await;
}
}
Ok(())
}

View File

@ -210,4 +210,5 @@ pub struct Reminder {
pub id: u32,
pub name: String,
pub time: u32,
pub channel_id: u32,
}