9
9
using Terraria . Localization ;
10
10
using TerrariaApi . Server ;
11
11
using TShockAPI ;
12
+ using TShockAPI . DB ;
12
13
13
14
14
15
namespace CaiBot ;
@@ -19,15 +20,16 @@ public class Plugin : TerrariaPlugin
19
20
public override string Author => "Cai,羽学,西江" ;
20
21
public override string Description => "CaiBot机器人的适配插件" ;
21
22
public override string Name => "CaiBotPlugin" ;
22
- public static readonly Version VersionNum = new ( 2024 , 10 , 20 , 1 ) ; //日期+版本号(0,1,2...)
23
+ public static readonly Version VersionNum = new ( 2024 , 11 , 3 , 1 ) ; //日期+版本号(0,1,2...)
23
24
public override Version Version => VersionNum ;
24
25
25
26
public Plugin ( Main game ) : base ( game )
26
27
{
27
28
}
28
29
29
30
public static int InitCode = - 1 ;
30
-
31
+ public static bool LocalMode ;
32
+ public static bool DebugMode ;
31
33
public static ClientWebSocket WebSocket = new ( ) ;
32
34
public static Task WebSocketTask = Task . CompletedTask ;
33
35
public static readonly CancellationTokenSource TokenSource = new ( ) ;
@@ -54,8 +56,11 @@ public Plugin(Main game) : base(game)
54
56
55
57
public override void Initialize ( )
56
58
{
57
- // Commands.ChatCommands.Add(new Command( TestCommand,"test "));
59
+ Commands . ChatCommands . Add ( new Command ( "CaiBot.Admin" , this . CaiBotCommand , "caibot ") ) ;
58
60
Config . Read ( ) ;
61
+ LocalMode = Terraria . Program . LaunchParameters . ContainsKey ( "-cailocalbot" ) ;
62
+ DebugMode = Terraria . Program . LaunchParameters . ContainsKey ( "-caidebug" ) ;
63
+ BanManager . OnBanPostAdd += this . OnBanInsert ;
59
64
AppDomain . CurrentDomain . AssemblyResolve += this . CurrentDomain_AssemblyResolve ;
60
65
On . OTAPI . Hooks . MessageBuffer . InvokeGetData += this . MessageBuffer_InvokeGetData ;
61
66
On . OTAPI . Hooks . MessageBuffer . InvokeGetData += Login . MessageBuffer_InvokeGetData ;
@@ -68,7 +73,7 @@ public override void Initialize()
68
73
try
69
74
{
70
75
WebSocket = new ClientWebSocket ( ) ;
71
- while ( Config . config . Token == "" )
76
+ while ( string . IsNullOrEmpty ( Config . config . Token ) )
72
77
{
73
78
await Task . Delay ( TimeSpan . FromSeconds ( 10 ) ) ;
74
79
HttpClient client = new ( ) ;
@@ -90,10 +95,11 @@ public override void Initialize()
90
95
91
96
}
92
97
93
- if ( Terraria . Program . LaunchParameters . ContainsKey ( "-cailocalbot" ) )
98
+ if ( LocalMode )
94
99
{
95
100
await WebSocket . ConnectAsync ( new Uri ( "ws://127.0.0.1:22334/bot/" + Config . config . Token ) ,
96
101
CancellationToken . None ) ;
102
+ TShock . Log . ConsoleWarn ( $ "[CaiAPI]你正在使用CaiBot本地模式,请确保你已本地部署CaiBot!") ;
97
103
}
98
104
else
99
105
{
@@ -107,7 +113,7 @@ await WebSocket.ConnectAsync(new Uri("ws://api.terraria.ink:22334/bot/" + Config
107
113
var result = await WebSocket . ReceiveAsync ( new ArraySegment < byte > ( buffer ) ,
108
114
CancellationToken . None ) ;
109
115
var receivedData = Encoding . UTF8 . GetString ( buffer , 0 , result . Count ) ;
110
- if ( Terraria . Program . LaunchParameters . ContainsKey ( "-caidebug" ) )
116
+ if ( DebugMode )
111
117
{
112
118
TShock . Log . ConsoleInfo ( $ "[CaiAPI]收到BOT数据包: { receivedData } ") ;
113
119
}
@@ -118,7 +124,7 @@ await WebSocket.ConnectAsync(new Uri("ws://api.terraria.ink:22334/bot/" + Config
118
124
catch ( Exception ex )
119
125
{
120
126
TShock . Log . ConsoleInfo ( $ "[CaiAPI]CaiBot断开连接...") ;
121
- if ( Terraria . Program . LaunchParameters . ContainsKey ( "-caidebug" ) )
127
+ if ( DebugMode )
122
128
{
123
129
TShock . Log . ConsoleError ( ex . ToString ( ) ) ;
124
130
}
@@ -160,11 +166,15 @@ protected override void Dispose(bool disposing)
160
166
{
161
167
if ( disposing )
162
168
{
169
+ var asm = Assembly . GetExecutingAssembly ( ) ;
170
+ Commands . ChatCommands . RemoveAll ( c => c . CommandDelegate . Method ? . DeclaringType ? . Assembly == asm ) ;
163
171
AppDomain . CurrentDomain . AssemblyResolve -= this . CurrentDomain_AssemblyResolve ;
164
172
On . OTAPI . Hooks . MessageBuffer . InvokeGetData -= this . MessageBuffer_InvokeGetData ;
165
173
On . OTAPI . Hooks . MessageBuffer . InvokeGetData -= Login . MessageBuffer_InvokeGetData ;
174
+ BanManager . OnBanPostAdd -= this . OnBanInsert ;
166
175
ServerApi . Hooks . NetGetData . Deregister ( this , Login . OnGetData ) ;
167
176
ServerApi . Hooks . GamePostInitialize . Deregister ( this , this . GenCode ) ;
177
+ WebSocket . Dispose ( ) ;
168
178
if ( ! WebSocketTask . IsCompleted )
169
179
{
170
180
TokenSource . Cancel ( ) ;
@@ -173,6 +183,105 @@ protected override void Dispose(bool disposing)
173
183
}
174
184
base . Dispose ( disposing ) ;
175
185
}
186
+
187
+ private void CaiBotCommand ( CommandArgs args )
188
+ {
189
+ var plr = args . Player ;
190
+ void ShowHelpText ( )
191
+ {
192
+ if ( ! PaginationTools . TryParsePageNumber ( args . Parameters , 1 , plr , out var pageNumber ) )
193
+ {
194
+ return ;
195
+ }
196
+
197
+ List < string > lines = new ( )
198
+ {
199
+ "/caibot debug CaiBot调试开关" ,
200
+ "/caibot code 生成并且展示验证码" ,
201
+ "/caibot info 显示CaiBot的一些信息"
202
+ } ;
203
+
204
+ PaginationTools . SendPage (
205
+ plr , pageNumber , lines ,
206
+ new PaginationTools . Settings
207
+ {
208
+ HeaderFormat = GetString ( "帮助 ({0}/{1}):" ) ,
209
+ FooterFormat = GetString ( "输入 {0}caibot help {{0}} 查看更多" ) . SFormat ( Commands . Specifier )
210
+ }
211
+ ) ;
212
+ }
213
+
214
+ if ( args . Parameters . Count == 0 )
215
+ {
216
+ ShowHelpText ( ) ;
217
+ return ;
218
+ }
219
+
220
+
221
+ switch ( args . Parameters [ 0 ] . ToLowerInvariant ( ) )
222
+ {
223
+ // 帮助
224
+ case "help" :
225
+ ShowHelpText ( ) ;
226
+ return ;
227
+
228
+ default :
229
+ ShowHelpText ( ) ;
230
+ break ;
231
+
232
+ case "信息" :
233
+ case "info" :
234
+ plr . SendInfoMessage ( $ "[CaiBot信息]\n " +
235
+ $ "插件版本: v{ VersionNum } \n " +
236
+ $ "WebSocket状态: { WebSocket . State } \n " +
237
+ $ "绑定QQ群: { ( Config . config . GroupNumber == 0L ? "未绑定|未连接" : Config . config . GroupNumber ) } \n " +
238
+ $ "本地模式: { LocalMode } \n " +
239
+ $ "绑定状态: { Config . config . Token != "" } \n " +
240
+ $ "Debug模式: { DebugMode } \n " +
241
+ $ "Economic API支持: { EconomicSupport . GetCoinsSupport } \n "+
242
+ $ "Economic RPG支持: { EconomicSupport . GetLevelNameSupport } \n "+
243
+ $ "Economic Skill支持: { EconomicSupport . GetSkillSupport } \n "
244
+ ) ;
245
+ break ;
246
+ case "调试" :
247
+ case "debug" :
248
+ DebugMode = ! DebugMode ;
249
+ plr . SendInfoMessage ( $ "[CaiBot]调试模式已{ ( DebugMode ? "开启" : "关闭" ) } !") ;
250
+ break ;
251
+ case "验证码" :
252
+ case "code" :
253
+ if ( ! string . IsNullOrEmpty ( Config . config . Token ) )
254
+ {
255
+ plr . SendInfoMessage ( $ "[CaiBot]服务器已绑定无法生成验证码!") ;
256
+ return ;
257
+ }
258
+ this . GenCode ( null ) ;
259
+ plr . SendInfoMessage ( $ "[CaiBot]验证码已生成,请在后台查看喵~") ;
260
+ break ;
261
+ }
262
+ }
263
+
264
+ private async void OnBanInsert ( object ? sender , BanEventArgs e )
265
+ {
266
+ if ( e . Ban . Identifier . StartsWith ( Identifier . Name . Prefix ) || e . Ban . Identifier . StartsWith ( Identifier . Account . Prefix ) )
267
+ {
268
+ var name = e . Ban . Identifier . Replace ( Identifier . Name . Prefix , "" ) . Replace ( Identifier . Account . Prefix , "" ) ;
269
+ var expireTime = e . Ban . GetPrettyExpirationString ( ) ;
270
+ var result = new RestObject
271
+ {
272
+ { "type" , "post_ban_add" } ,
273
+ { "name" , name } ,
274
+ { "reason" , e . Ban . Reason } ,
275
+ { "admin" , e . Ban . BanningUser } ,
276
+ { "expire_time" , expireTime == "Never" ? "永久封禁" : expireTime }
277
+ } ;
278
+ await MessageHandle . SendDateAsync ( JsonConvert . SerializeObject ( result ) ) ;
279
+ }
280
+
281
+
282
+ }
283
+
284
+
176
285
private void GenCode ( EventArgs args )
177
286
{
178
287
if ( ! string . IsNullOrEmpty ( Config . config . Token ) )
0 commit comments