|
| 1 | +use anyhow::anyhow; |
| 2 | +use async_trait::async_trait; |
| 3 | +use countdown_bot3::{ |
| 4 | + countdown_bot::{ |
| 5 | + bot, |
| 6 | + client::{CountdownBotClient, ResultType}, |
| 7 | + command::{Command, SenderType}, |
| 8 | + plugin::{BotPlugin, HookResult, PluginMeta}, |
| 9 | + utils::load_config_or_save_default, |
| 10 | + }, |
| 11 | + export_static_plugin, |
| 12 | +}; |
| 13 | +use jieba_rs::Jieba; |
| 14 | +use log::{debug, error}; |
| 15 | +use serde::{Deserialize, Serialize}; |
| 16 | +use std::{collections::HashMap, sync::Arc}; |
| 17 | +static PLUGIN_NAME: &str = "genshin_saying"; |
| 18 | + |
| 19 | +#[derive(Deserialize, Serialize)] |
| 20 | +pub struct GenshinSayingConfig { |
| 21 | + pub xamaran_template: String, |
| 22 | +} |
| 23 | + |
| 24 | +impl Default for GenshinSayingConfig { |
| 25 | + fn default() -> Self { |
| 26 | + Self { |
| 27 | + xamaran_template: "今日{xx},明日{xx},百日之后{yy}中已不剩{yy},只剩{xx}。".into(), |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[derive(Default)] |
| 33 | +struct GenshinSayingPlugin { |
| 34 | + client: Option<CountdownBotClient>, |
| 35 | + config: Option<GenshinSayingConfig>, |
| 36 | + jieba: Arc<Jieba>, |
| 37 | +} |
| 38 | + |
| 39 | +#[async_trait] |
| 40 | +impl BotPlugin for GenshinSayingPlugin { |
| 41 | + fn on_enable( |
| 42 | + &mut self, |
| 43 | + bot: &mut bot::CountdownBot, |
| 44 | + _handle: tokio::runtime::Handle, |
| 45 | + ) -> HookResult<()> { |
| 46 | + self.config = Some(load_config_or_save_default::<GenshinSayingConfig>( |
| 47 | + &bot.ensure_plugin_data_dir(PLUGIN_NAME)?, |
| 48 | + )?); |
| 49 | + bot.register_command( |
| 50 | + Command::new("xamaran") |
| 51 | + .description("生成赞玛兰风格语录: xamaran <xx> <yy>") |
| 52 | + .enable_all(), |
| 53 | + )?; |
| 54 | + bot.register_command( |
| 55 | + Command::new("neko") |
| 56 | + .description("生成寝子风格语录(对我、你等文字进行替换): neko <文本内容>") |
| 57 | + .enable_all(), |
| 58 | + )?; |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | + fn on_before_start( |
| 62 | + &mut self, |
| 63 | + _bot: &mut bot::CountdownBot, |
| 64 | + client: CountdownBotClient, |
| 65 | + ) -> HookResult<()> { |
| 66 | + self.client = Some(client); |
| 67 | + Ok(()) |
| 68 | + } |
| 69 | + fn get_meta(&self) -> PluginMeta { |
| 70 | + PluginMeta { |
| 71 | + author: String::from("officeyutong"), |
| 72 | + description: String::from("赞玛兰风格语录与寝子风格语录"), |
| 73 | + version: env!("CARGO_PKG_VERSION").to_string(), |
| 74 | + } |
| 75 | + } |
| 76 | + async fn on_command( |
| 77 | + &mut self, |
| 78 | + command: String, |
| 79 | + args: Vec<String>, |
| 80 | + sender: &SenderType, |
| 81 | + ) -> Result<(), Box<dyn std::error::Error>> { |
| 82 | + match command.as_str() { |
| 83 | + "xamaran" => self.xamaran(&args, sender).await?, |
| 84 | + "neko" => self.neko(&args, sender).await?, |
| 85 | + _ => {} |
| 86 | + }; |
| 87 | + Ok(()) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl GenshinSayingPlugin { |
| 92 | + async fn xamaran(&self, args: &Vec<String>, sender: &SenderType) -> ResultType<()> { |
| 93 | + let client = self.client.as_ref().unwrap(); |
| 94 | + if let [x, y] = &args[..] { |
| 95 | + let cfg_str = self.config.as_ref().unwrap().xamaran_template.clone(); |
| 96 | + let mut hmap = HashMap::<String, String>::new(); |
| 97 | + hmap.insert("xx".into(), x.into()); |
| 98 | + hmap.insert("yy".into(), y.into()); |
| 99 | + |
| 100 | + let result = strfmt::strfmt(&cfg_str, &hmap).map_err(|e| { |
| 101 | + error!("非法格式控制字符串 {}: {}", e, cfg_str); |
| 102 | + anyhow!("非法格式控制字符串!") |
| 103 | + })?; |
| 104 | + client.quick_send_by_sender(sender, &result).await?; |
| 105 | + return Ok(()); |
| 106 | + } else { |
| 107 | + return Err(anyhow!("需要两个参数!").into()); |
| 108 | + } |
| 109 | + } |
| 110 | + async fn neko(&self, args: &Vec<String>, sender: &SenderType) -> ResultType<()> { |
| 111 | + let text = args.join(" "); |
| 112 | + // let splitted = |
| 113 | + // let jieba = Jieba::new(); |
| 114 | + debug!("Nekonize input: {:?}", text); |
| 115 | + let jieba = self.jieba.clone(); |
| 116 | + let result: anyhow::Result<String> = tokio::task::spawn_blocking(move || { |
| 117 | + let words = jieba.cut(&text, false); |
| 118 | + debug!("Splitted words: {:?}", words); |
| 119 | + let result = words |
| 120 | + .iter() |
| 121 | + .map(|s| match *s { |
| 122 | + "我" => "奴家".into(), |
| 123 | + "你" => "汝等".into(), |
| 124 | + "。" => ",喵喵。".into(), |
| 125 | + t => t.to_string(), |
| 126 | + }) |
| 127 | + .collect::<Vec<String>>() |
| 128 | + .join(""); |
| 129 | + // words |
| 130 | + Ok(result) |
| 131 | + }) |
| 132 | + .await?; |
| 133 | + let client = self.client.as_ref().unwrap(); |
| 134 | + |
| 135 | + client.quick_send_by_sender(sender, &result?).await?; |
| 136 | + return Ok(()); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export_static_plugin!(PLUGIN_NAME, GenshinSayingPlugin::default()); |
0 commit comments