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

View File

@ -19,7 +19,6 @@ use regex::Regex;
use chrono_tz::Tz;
use crate::{
consts::MAX_MESSAGE_LENGTH,
models::{
ChannelData,
UserData,
@ -27,6 +26,7 @@ use crate::{
},
SQLPool,
FrameworkCtx,
framework::SendIterator,
};
use std::iter;
@ -270,23 +270,15 @@ SELECT name, command FROM command_aliases WHERE guild_id = (SELECT id FROM guild
.await
.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();
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;
}
let _ = msg.channel_id.say_lines(&ctx, content).await;
},
"remove" => {

View File

@ -1,6 +1,8 @@
use async_trait::async_trait;
use serenity::{
http::Http,
Result as SerenityResult,
client::Context,
framework::{
Framework,
@ -40,6 +42,8 @@ use crate::{
SQLPool,
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>;
@ -143,6 +147,34 @@ pub struct RegexFramework {
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 {
pub fn new(client_id: u64) -> Self {
Self {