Skip to content

Commit

Permalink
plugin simple_rand: add random choice
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong committed Jul 28, 2022
1 parent 6c68a05 commit 6f3b893
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/simple_rand/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "simple_rand"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
56 changes: 52 additions & 4 deletions plugins/simple_rand/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use countdown_bot3::{
initialize_plugin_logger,
// initialize_plugin_logger,
};
use rand::{prelude::StdRng, SeedableRng};
use rand::{
prelude::{SliceRandom, StdRng},
SeedableRng,
};
use serde::{Deserialize, Serialize};
use std::{path::PathBuf, str::FromStr};
#[derive(Deserialize, Serialize, Debug)]
Expand Down Expand Up @@ -58,6 +61,15 @@ impl BotPlugin for SimpleRandPlugin {
.guild(true)
.single_alias("随机"),
)?;
bot.register_command(
Command::new("choice")
.description("随机选择 | choice <选项1> [选项2 [选项3 [...]]]")
.console(true)
.group(true)
.private(true)
.guild(true)
.single_alias("随机选择"),
)?;
self.config = Some(load_config_or_save_default::<SimpleRandConfig>(
&self.plugin_data_root.as_ref().unwrap(),
)?);
Expand All @@ -80,9 +92,47 @@ impl BotPlugin for SimpleRandPlugin {
}
async fn on_command(
&mut self,
_command: String,
command: String,
args: Vec<String>,
sender: &SenderType,
) -> Result<(), Box<dyn std::error::Error>> {
match command.as_str() {
"rand" => self.handle_rand(&args, sender).await?,
"choice" => self.handle_choice(&args, sender).await?,
_ => {}
};
return Ok(());
}
}

countdown_bot3::export_static_plugin!(PLUGIN_NAME, SimpleRandPlugin::new());

impl SimpleRandPlugin {
async fn handle_choice(
&self,
args: &Vec<String>,
sender: &SenderType,
) -> Result<(), Box<dyn std::error::Error>> {
if args.len() < 1 {
return Err(anyhow!("请输入最少一个选项!").into());
}
let max_count = self.config.as_ref().unwrap().max_number_count;
if args.len() > max_count as usize {
return Err(anyhow!("最多允许 {} 个随机选项!", max_count).into());
}
let mut rng: StdRng = SeedableRng::from_entropy();
let elem = args.choose(&mut rng).unwrap();
self.client
.as_ref()
.unwrap()
.quick_send_by_sender(&sender, format!("你的选择结果是:\n{}", elem).as_str())
.await?;
return Ok(());
}
async fn handle_rand(
&self,
args: &Vec<String>,
sender: &SenderType,
) -> Result<(), Box<dyn std::error::Error>> {
if args.len() == 0 {
return Err(anyhow!("请输入至少一个参数!").into());
Expand Down Expand Up @@ -119,5 +169,3 @@ impl BotPlugin for SimpleRandPlugin {
Ok(())
}
}

countdown_bot3::export_static_plugin!(PLUGIN_NAME, SimpleRandPlugin::new());

0 comments on commit 6f3b893

Please sign in to comment.