turned say_lines into a method of channelid

This commit is contained in:
jude 2020-10-01 18:07:27 +01:00
parent 1813a00eac
commit 74f1c7aada
4 changed files with 70 additions and 23 deletions

33
Cargo.lock generated
View File

@ -211,9 +211,9 @@ dependencies = [
[[package]] [[package]]
name = "command_attr" name = "command_attr"
version = "0.3.0-rc.1" version = "0.3.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c538daab2daaf13de61cea91648a62bb11d267ef629f707d5fe3dd080043ab4d" checksum = "6745770e89e5e4583424362f15f91c8bb0ef54387d209af30e0446d08909ae7d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -1312,9 +1312,9 @@ dependencies = [
[[package]] [[package]]
name = "serenity" name = "serenity"
version = "0.9.0-rc.1" version = "0.9.0-rc.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21e935a7f3f4752257183ee1f3553b10ea5b514a55de0e536dca7f3742b97d18" checksum = "d179c8684cccfc95898c4a97ba3cb8787dd8db5e3f8cb645efd129ddb1a7dd7f"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"async-tungstenite", "async-tungstenite",
@ -1325,12 +1325,13 @@ dependencies = [
"command_attr", "command_attr",
"flate2", "flate2",
"futures", "futures",
"log",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
"static_assertions", "static_assertions",
"tokio", "tokio",
"tracing",
"tracing-futures",
"typemap_rev", "typemap_rev",
"url", "url",
"uwl", "uwl",
@ -1641,9 +1642,21 @@ checksum = "f0aae59226cf195d8e74d4b34beae1859257efb4e5fed3f147d2dc2c7d372178"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"log", "log",
"tracing-attributes",
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-attributes"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "tracing-core" name = "tracing-core"
version = "0.1.12" version = "0.1.12"
@ -1653,6 +1666,16 @@ dependencies = [
"lazy_static", "lazy_static",
] ]
[[package]]
name = "tracing-futures"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab7bb6f14721aa00656086e9335d363c5c8747bae02ebe32ea2c7dece5689b4c"
dependencies = [
"pin-project",
"tracing",
]
[[package]] [[package]]
name = "try-lock" name = "try-lock"
version = "0.2.3" version = "0.2.3"

View File

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

View File

@ -19,7 +19,6 @@ use regex::Regex;
use chrono_tz::Tz; use chrono_tz::Tz;
use crate::{ use crate::{
consts::MAX_MESSAGE_LENGTH,
models::{ models::{
ChannelData, ChannelData,
UserData, UserData,
@ -27,6 +26,7 @@ use crate::{
}, },
SQLPool, SQLPool,
FrameworkCtx, FrameworkCtx,
framework::SendIterator,
}; };
use std::iter; use std::iter;
@ -270,23 +270,15 @@ SELECT name, command FROM command_aliases WHERE guild_id = (SELECT id FROM guild
.await .await
.unwrap(); .unwrap();
let content = iter::once("Aliases:".to_string()).chain(aliases.iter().map(|row| format!("**{}**: `{}`", row.name, row.command))); let content = iter::once("Aliases:".to_string())
.chain(
aliases
.iter()
.map(|row| format!("**{}**: `{}`", row.name, row.command)
)
);
let mut current_content = String::new(); let _ = msg.channel_id.say_lines(&ctx, content).await;
for line in content {
if current_content.len() + line.len() > MAX_MESSAGE_LENGTH {
let _ = msg.channel_id.say(&ctx, &current_content).await;
current_content = line;
}
else {
current_content = format!("{}\n{}", current_content, line);
}
}
if !current_content.is_empty() {
let _ = msg.channel_id.say(&ctx, &current_content).await;
}
}, },
"remove" => { "remove" => {

View File

@ -1,6 +1,8 @@
use async_trait::async_trait; use async_trait::async_trait;
use serenity::{ use serenity::{
http::Http,
Result as SerenityResult,
client::Context, client::Context,
framework::{ framework::{
Framework, Framework,
@ -40,6 +42,8 @@ use crate::{
SQLPool, SQLPool,
consts::PREFIX, consts::PREFIX,
}; };
use serenity::model::id::ChannelId;
use crate::consts::MAX_MESSAGE_LENGTH;
type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, CommandResult>; type CommandFn = for<'fut> fn(&'fut Context, &'fut Message, String) -> BoxFuture<'fut, CommandResult>;
@ -143,6 +147,34 @@ pub struct RegexFramework {
ignore_bots: bool, ignore_bots: bool,
} }
#[async_trait]
pub trait SendIterator {
async fn say_lines(self, http: impl AsRef<Http> + Send + Sync + 'async_trait, content: impl Iterator<Item=String> + Send + 'async_trait) -> SerenityResult<()>;
}
#[async_trait]
impl SendIterator for ChannelId {
async fn say_lines(self, http: impl AsRef<Http> + Send + Sync + 'async_trait, content: impl Iterator<Item=String> + Send + 'async_trait) -> SerenityResult<()> {
let mut current_content = String::new();
for line in content {
if current_content.len() + line.len() > MAX_MESSAGE_LENGTH {
self.say(&http, &current_content).await?;
current_content = line;
}
else {
current_content = format!("{}\n{}", current_content, line);
}
}
if !current_content.is_empty() {
self.say(&http, &current_content).await?;
}
Ok(())
}
}
impl RegexFramework { impl RegexFramework {
pub fn new(client_id: u64) -> Self { pub fn new(client_id: u64) -> Self {
Self { Self {