Skip to content

Commit 6f3b893

Browse files
committed
plugin simple_rand: add random choice
1 parent 6c68a05 commit 6f3b893

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/simple_rand/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "simple_rand"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

plugins/simple_rand/src/lib.rs

+52-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use countdown_bot3::{
1010
initialize_plugin_logger,
1111
// initialize_plugin_logger,
1212
};
13-
use rand::{prelude::StdRng, SeedableRng};
13+
use rand::{
14+
prelude::{SliceRandom, StdRng},
15+
SeedableRng,
16+
};
1417
use serde::{Deserialize, Serialize};
1518
use std::{path::PathBuf, str::FromStr};
1619
#[derive(Deserialize, Serialize, Debug)]
@@ -58,6 +61,15 @@ impl BotPlugin for SimpleRandPlugin {
5861
.guild(true)
5962
.single_alias("随机"),
6063
)?;
64+
bot.register_command(
65+
Command::new("choice")
66+
.description("随机选择 | choice <选项1> [选项2 [选项3 [...]]]")
67+
.console(true)
68+
.group(true)
69+
.private(true)
70+
.guild(true)
71+
.single_alias("随机选择"),
72+
)?;
6173
self.config = Some(load_config_or_save_default::<SimpleRandConfig>(
6274
&self.plugin_data_root.as_ref().unwrap(),
6375
)?);
@@ -80,9 +92,47 @@ impl BotPlugin for SimpleRandPlugin {
8092
}
8193
async fn on_command(
8294
&mut self,
83-
_command: String,
95+
command: String,
8496
args: Vec<String>,
8597
sender: &SenderType,
98+
) -> Result<(), Box<dyn std::error::Error>> {
99+
match command.as_str() {
100+
"rand" => self.handle_rand(&args, sender).await?,
101+
"choice" => self.handle_choice(&args, sender).await?,
102+
_ => {}
103+
};
104+
return Ok(());
105+
}
106+
}
107+
108+
countdown_bot3::export_static_plugin!(PLUGIN_NAME, SimpleRandPlugin::new());
109+
110+
impl SimpleRandPlugin {
111+
async fn handle_choice(
112+
&self,
113+
args: &Vec<String>,
114+
sender: &SenderType,
115+
) -> Result<(), Box<dyn std::error::Error>> {
116+
if args.len() < 1 {
117+
return Err(anyhow!("请输入最少一个选项!").into());
118+
}
119+
let max_count = self.config.as_ref().unwrap().max_number_count;
120+
if args.len() > max_count as usize {
121+
return Err(anyhow!("最多允许 {} 个随机选项!", max_count).into());
122+
}
123+
let mut rng: StdRng = SeedableRng::from_entropy();
124+
let elem = args.choose(&mut rng).unwrap();
125+
self.client
126+
.as_ref()
127+
.unwrap()
128+
.quick_send_by_sender(&sender, format!("你的选择结果是:\n{}", elem).as_str())
129+
.await?;
130+
return Ok(());
131+
}
132+
async fn handle_rand(
133+
&self,
134+
args: &Vec<String>,
135+
sender: &SenderType,
86136
) -> Result<(), Box<dyn std::error::Error>> {
87137
if args.len() == 0 {
88138
return Err(anyhow!("请输入至少一个参数!").into());
@@ -119,5 +169,3 @@ impl BotPlugin for SimpleRandPlugin {
119169
Ok(())
120170
}
121171
}
122-
123-
countdown_bot3::export_static_plugin!(PLUGIN_NAME, SimpleRandPlugin::new());

0 commit comments

Comments
 (0)