1 Commits
code ... 0.5.0

Author SHA1 Message Date
3085d4c450 Merge pull request #8 from house-of-vanity/code
Code
2021-01-08 17:38:56 +03:00
5 changed files with 24 additions and 43 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "desubot" name = "desubot"
version = "0.5.1" version = "0.5.0"
authors = ["AB <ab@hexor.ru>"] authors = ["AB <ab@hexor.ru>"]
edition = "2018" edition = "2018"
@ -30,8 +30,7 @@ subprocess = "0.2.6"
serde_json = "1.0" serde_json = "1.0"
markov = "1.1.0" markov = "1.1.0"
rand = "0.7.3" rand = "0.7.3"
mystem = "^0.2" mystem = "0.2.1"
#mystem = { path = "../mystem-rs" }
async-trait = "0.1.42" async-trait = "0.1.42"
sqlparser = "0.7.0" sqlparser = "0.7.0"

View File

@ -1,10 +1,10 @@
#[allow(dead_code)] static CODE: &str = "<b>Code highlighter</b>
static CODE_HELP: &str = "<b>Code highlighter</b>
<i>Usage</i> <i>Usage</i>
<pre>/CODE <pre>/CODE
#&lt;theme - Dracula by default&gt;
&lt;CODE&gt; &lt;CODE&gt;
#&lt;lang - JS by default&gt; #&lt;theme - Dracula by default&gt;</pre> #&lt;lang - JS by default&gt;</pre>
Language may be defined by both name and extension - Rust, rs... Language may be defined by both name and extension - Rust, rs...
Max lines - 80 Max lines - 80
@ -34,10 +34,3 @@ gruvbox-light
gruvbox-white gruvbox-white
zenburn zenburn
"; ";
#[allow(dead_code)]
pub static SQL_HELP: &str = "<b>Perform an SQL command</b>
<i>* Only one sentence per message.
* Only SELECT command.
* Max result length is 100 lines. Use LIMIT 100.
* SQLITE syntax is available only.</i>";

View File

@ -17,13 +17,11 @@ use sqlparser::ast::Statement;
use sqlparser::dialect::GenericDialect; use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser; use sqlparser::parser::Parser;
use syntect::easy::HighlightLines; use syntect::easy::HighlightLines;
use syntect::highlighting::Theme;
use syntect::parsing::SyntaxReference; use syntect::parsing::SyntaxReference;
use syntect::util::LinesWithEndings; use syntect::util::LinesWithEndings;
use telegram_bot::prelude::*; use telegram_bot::prelude::*;
use telegram_bot::{Api, Message, ParseMode}; use telegram_bot::{Api, Message, ParseMode};
use syntect::highlighting::Theme;
include!("../assets/help_text.rs");
pub struct Here { pub struct Here {
pub data: String, pub data: String,
@ -67,10 +65,6 @@ impl Execute for Sql {
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> { async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
let mut sql = self.data.clone(); let mut sql = self.data.clone();
debug!("PIZDA - {}", sql);
if sql == "/sql" || sql == "/sql-" {
return Ok(SQL_HELP.to_string())
}
let is_head = if sql.starts_with('-') { let is_head = if sql.starts_with('-') {
sql = sql.replacen("-", "", 1); sql = sql.replacen("-", "", 1);
false false
@ -143,7 +137,7 @@ impl Execute for Sql {
} }
res.push(tmp); res.push(tmp);
} }
if res.len() >= 100 { if res.len() > 100 {
return Err(Error::SQLResultTooLong( return Err(Error::SQLResultTooLong(
"SQL result too long. Lines limit is 100. Use LIMIT".to_string(), "SQL result too long. Lines limit is 100. Use LIMIT".to_string(),
)); ));
@ -557,16 +551,11 @@ impl Execute for Code {
unimplemented!() unimplemented!()
} }
async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> { async fn exec_with_result(&self, api: &Api, message: &Message) -> Result<String, Error> {
let mut lines: Vec<String> = self let mut lines: Vec<String> = self.data.trim().split("\n").map(|s| s.to_string()).collect();
.data
.trim()
.split("\n")
.map(|s| s.to_string())
.collect();
if lines.len() >= 81 { if lines.len() >= 81 {
return Err(CodeHighlightningError); return Err(CodeHighlightningError);
} }
let last_line = &lines[lines.len() - 1]; let last_line = &lines[lines.len()-1];
let tags = last_line let tags = last_line
.trim() .trim()
@ -604,6 +593,7 @@ impl Execute for Code {
.map(|s| s.unwrap()) .map(|s| s.unwrap())
.collect(); .collect();
let theme = if theme.len() != 1 { let theme = if theme.len() != 1 {
ts.themes.get("Dracula").unwrap() ts.themes.get("Dracula").unwrap()
} else { } else {
@ -616,7 +606,6 @@ impl Execute for Code {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let formatter = silicon::formatter::ImageFormatterBuilder::<String>::new() let formatter = silicon::formatter::ImageFormatterBuilder::<String>::new()
.window_controls(false) .window_controls(false)
.line_offset(1)
.round_corner(false); .round_corner(false);
let mut formatter = formatter.build().unwrap(); let mut formatter = formatter.build().unwrap();
let image = formatter.format(&highlight, &theme); let image = formatter.format(&highlight, &theme);

View File

@ -214,7 +214,7 @@ pub(crate) async fn add_conf(message: Message) -> Result<(), Error> {
match get_conf(message.chat.id()) { match get_conf(message.chat.id()) {
Ok(_) => { Ok(_) => {
debug!("Group found: {:?}", message.chat.id()); //info!("Group found: {:?}", message.chat.id());
let update = Conf { let update = Conf {
id: message.chat.id(), id: message.chat.id(),
title, title,
@ -228,10 +228,10 @@ pub(crate) async fn add_conf(message: Message) -> Result<(), Error> {
id = :id", id = :id",
)?; )?;
stmt.execute_named(&[(":id", &update.id.to_string()), (":title", &update.title)])?; stmt.execute_named(&[(":id", &update.id.to_string()), (":title", &update.title)])?;
debug!("Conf {:?} updated: {:?}", update.title, get_conf(update.id)); //info!("Conf {:?} updated: {:?}", update.title, get_conf(update.id));
} }
Err(_) => { Err(_) => {
debug!("Group didn't found: {:?}", message.chat.id()); //info!("Group didn't found: {:?}", message.chat.id());
let update = Conf { let update = Conf {
id: message.chat.id(), id: message.chat.id(),
@ -250,7 +250,7 @@ pub(crate) async fn add_conf(message: Message) -> Result<(), Error> {
(":title", &update.title), (":title", &update.title),
(":date", &unix_time), (":date", &unix_time),
])?; ])?;
debug!("Conf {:?} added: {:?}", update.title, get_conf(update.id)); //info!("Conf {:?} added: {:?}", update.title, get_conf(update.id));
} }
} }
Ok(()) Ok(())
@ -283,7 +283,7 @@ pub(crate) async fn add_user(message: Message) -> Result<(), Error> {
(":first_name", &update.first_name), (":first_name", &update.first_name),
(":last_name", &update.last_name), (":last_name", &update.last_name),
])?; ])?;
debug!("User {} updated: {:?}", update.first_name, get_user(update.id)); //println!("User {} updated: {:?}", update.first_name, get_user(user.id));
} }
Err(_) => { Err(_) => {
let unix_time = SystemTime::now() let unix_time = SystemTime::now()
@ -310,7 +310,7 @@ pub(crate) async fn add_user(message: Message) -> Result<(), Error> {
(":last_name", &user.last_name), (":last_name", &user.last_name),
(":date", &unix_time), (":date", &unix_time),
])?; ])?;
debug!("User added: {:?}", user); //println!("User added: {:?}", user);
} }
} }
Ok(()) Ok(())

View File

@ -6,6 +6,7 @@ use crate::utils;
use mystem::MyStem; use mystem::MyStem;
use telegram_bot::*; use telegram_bot::*;
include!("../assets/help_text.rs"); include!("../assets/help_text.rs");
pub async fn handler( pub async fn handler(
@ -24,12 +25,13 @@ pub async fn handler(
title, title,
&message.from.id, &message.from.id,
&message.from.first_name, &message.from.first_name,
data.replace("\n", " ") {if data.len() <= 200 {data} else {&data[..200]}}.replace("\n", " ")
); );
let cleaned_message = data.replace(&format!("@{}", me.clone().username.unwrap()), ""); let cleaned_message = data.replace(&format!("@{}", me.clone().username.unwrap()), "");
match cleaned_message.as_str() { match cleaned_message.as_str() {
s if s.to_string().starts_with("/code") => { s if s.to_string().starts_with("/code") => {
match { match {
Code { Code {
data: s.replace("/code", ""), data: s.replace("/code", ""),
@ -48,7 +50,7 @@ pub async fn handler(
} }
Err(_) => { Err(_) => {
let _ = api let _ = api
.send(message.text_reply(CODE_HELP).parse_mode(ParseMode::Html)) .send(message.text_reply(CODE).parse_mode(ParseMode::Html))
.await?; .await?;
} }
} }
@ -118,9 +120,7 @@ pub async fn handler(
.exec_mystem(&api, &message, mystem) .exec_mystem(&api, &message, mystem)
.await? .await?
} }
_ => { _ => db::add_sentence(&message, mystem).await?,
db::add_sentence(&message, mystem).await?
}
} }
} }
MessageKind::Photo { ref caption, .. } => { MessageKind::Photo { ref caption, .. } => {