@@ -23,6 +23,7 @@ public class NodeBot
2323 public event EventHandler < ReceiveMessageEvent > ? ReceiveMessageEvent ;
2424 public List < ICommand > Commands = new List < ICommand > ( ) ;
2525 public List < IService > Services = new List < IService > ( ) ;
26+ public Queue < Task > ToDoQueue = new Queue < Task > ( ) ;
2627 public NodeBot ( string ip )
2728 {
2829 session = new ( new ( )
@@ -33,7 +34,7 @@ public NodeBot(string ip)
3334 } ) ;
3435 session . PostPipeline . Use ( async ( context , next ) =>
3536 {
36- if ( ReceiveMessageEvent != null )
37+ if ( ReceiveMessageEvent != null )
3738 {
3839 ReceiveMessageEvent ( this , new ( context ) ) ;
3940 }
@@ -45,16 +46,19 @@ public NodeBot(string ip)
4546 } ;
4647 ReceiveMessageEvent += ( sender , e ) =>
4748 {
48- if ( e . Context is CqPrivateMessagePostContext cqPrivateMessage )
49+ if ( e . Context is CqPrivateMessagePostContext cqPrivateMessage )
4950 {
5051 ExecuteCommand ( new UserQQSender ( session , this , cqPrivateMessage . UserId ) , cqPrivateMessage . Message ) ;
5152 }
52- if ( e . Context is CqGroupMessagePostContext cqGroupMessage )
53+ if ( e . Context is CqGroupMessagePostContext cqGroupMessage )
5354 {
54- ExecuteCommand ( new GroupQQSender ( session , this , cqGroupMessage . GroupId , cqGroupMessage . UserId ) , cqGroupMessage . Message ) ;
55+ ExecuteCommand ( new GroupQQSender ( session , this , cqGroupMessage . GroupId , cqGroupMessage . UserId ) , cqGroupMessage . Message ) ;
5556 }
5657 } ;
5758 }
59+ /// <summary>
60+ /// 保存权限数据
61+ /// </summary>
5862 public void SavePermission ( )
5963 {
6064 if ( ! File . Exists ( "Permission.json" ) )
@@ -63,6 +67,9 @@ public void SavePermission()
6367 }
6468 File . WriteAllText ( "Permission.json" , Newtonsoft . Json . JsonConvert . SerializeObject ( Permissions ) ) ;
6569 }
70+ /// <summary>
71+ /// 加载权限数据
72+ /// </summary>
6673 public void LoadPermission ( )
6774 {
6875 if ( File . Exists ( "Permission.json" ) )
@@ -82,26 +89,39 @@ public void RegisterService(IService service)
8289 public void Start ( )
8390 {
8491 session . Start ( ) ;
85- foreach ( IService service in Services )
92+ foreach ( IService service in Services )
8693 {
8794 service . OnStart ( this ) ;
8895 }
96+ Task . Run ( ( ) =>
97+ {
98+ while ( true )
99+ {
100+ Task . Delay ( 1000 ) ;
101+ Task task ;
102+ lock ( ToDoQueue )
103+ {
104+ task = ToDoQueue . Dequeue ( ) ;
105+ }
106+ task . Start ( ) ;
107+ }
108+ } ) ;
89109 }
90110 public void CallConsoleInputEvent ( string text )
91111 {
92- if ( ConsoleInputEvent != null )
112+ if ( ConsoleInputEvent != null )
93113 {
94114 ConsoleInputEvent ( this , new ( text ) ) ;
95115 }
96116 }
97117 public void ExecuteCommand ( ICommandSender sender , string commandLine )
98118 {
99119 ICommand ? command = GetCommandByCommandLine ( commandLine ) ;
100- if ( command == null )
120+ if ( command == null )
101121 {
102122 return ;
103123 }
104- if ( sender is ConsoleCommandSender console )
124+ if ( sender is ConsoleCommandSender console )
105125 {
106126 if ( command . IsConsoleCommand ( ) )
107127 {
@@ -138,9 +158,9 @@ public void ExecuteCommand(IQQSender sender, CqMessage commandLine)
138158 public ICommand ? GetCommandByCommandLine ( string command )
139159 {
140160 string [ ] tmp = command . Split ( ' ' ) ;
141- foreach ( string s in tmp )
161+ foreach ( string s in tmp )
142162 {
143- if ( s != string . Empty )
163+ if ( s != string . Empty )
144164 {
145165 return FindCommand ( s ) ;
146166 }
@@ -149,9 +169,9 @@ public void ExecuteCommand(IQQSender sender, CqMessage commandLine)
149169 }
150170 public ICommand ? FindCommand ( string commandName )
151171 {
152- foreach ( ICommand command in Commands )
172+ foreach ( ICommand command in Commands )
153173 {
154- if ( command . GetName ( ) == commandName )
174+ if ( command . GetName ( ) == commandName )
155175 {
156176 return command ;
157177 }
@@ -169,15 +189,52 @@ public bool HasPermission(ICommand command, long QQNumber)
169189 }
170190 public bool HasPermission ( ICommand command , ICommandSender sender )
171191 {
172- if ( sender is IQQSender QQSender )
192+ if ( sender is IQQSender QQSender )
173193 {
174194 return HasPermission ( command , QQSender . GetNumber ( ) ) ;
175195 }
176- if ( sender is ConsoleCommandSender )
196+ if ( sender is ConsoleCommandSender )
177197 {
178198 return true ;
179199 }
180200 return false ;
181201 }
202+ public void RunTask ( Task task )
203+ {
204+ lock ( ToDoQueue )
205+ {
206+ ToDoQueue . Enqueue ( task ) ;
207+ }
208+ }
209+ public void RunAction ( Action action )
210+ {
211+ Task task = new ( action ) ;
212+ RunTask ( task ) ;
213+ }
214+ public void SendGroupMessage ( long GroupNumber , CqMessage msgs )
215+ {
216+ RunAction ( ( ) =>
217+ {
218+ session . SendGroupMessage ( GroupNumber , msgs ) ;
219+ } ) ;
220+ }
221+ public void SendPrivateMessage ( long QQNumber , CqMessage msgs )
222+ {
223+ RunAction ( ( ) =>
224+ {
225+ session . SendPrivateMessage ( QQNumber , msgs ) ;
226+ } ) ;
227+ }
228+ public void SendMessage ( long Number , CqMessage msgs , UserType type )
229+ {
230+ if ( type == UserType . User )
231+ {
232+ SendPrivateMessage ( Number , msgs ) ;
233+ }
234+ else if ( type == UserType . Group )
235+ {
236+ SendGroupMessage ( Number , msgs ) ;
237+ }
238+ }
182239 }
183240}
0 commit comments