@@ -10,7 +10,10 @@ use countdown_bot3::{
10
10
initialize_plugin_logger,
11
11
// initialize_plugin_logger,
12
12
} ;
13
- use rand:: { prelude:: StdRng , SeedableRng } ;
13
+ use rand:: {
14
+ prelude:: { SliceRandom , StdRng } ,
15
+ SeedableRng ,
16
+ } ;
14
17
use serde:: { Deserialize , Serialize } ;
15
18
use std:: { path:: PathBuf , str:: FromStr } ;
16
19
#[ derive( Deserialize , Serialize , Debug ) ]
@@ -58,6 +61,15 @@ impl BotPlugin for SimpleRandPlugin {
58
61
. guild ( true )
59
62
. single_alias ( "随机" ) ,
60
63
) ?;
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
+ ) ?;
61
73
self . config = Some ( load_config_or_save_default :: < SimpleRandConfig > (
62
74
& self . plugin_data_root . as_ref ( ) . unwrap ( ) ,
63
75
) ?) ;
@@ -80,9 +92,47 @@ impl BotPlugin for SimpleRandPlugin {
80
92
}
81
93
async fn on_command (
82
94
& mut self ,
83
- _command : String ,
95
+ command : String ,
84
96
args : Vec < String > ,
85
97
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 ,
86
136
) -> Result < ( ) , Box < dyn std:: error:: Error > > {
87
137
if args. len ( ) == 0 {
88
138
return Err ( anyhow ! ( "请输入至少一个参数!" ) . into ( ) ) ;
@@ -119,5 +169,3 @@ impl BotPlugin for SimpleRandPlugin {
119
169
Ok ( ( ) )
120
170
}
121
171
}
122
-
123
- countdown_bot3:: export_static_plugin!( PLUGIN_NAME , SimpleRandPlugin :: new( ) ) ;
0 commit comments