Skip to content

Commit 20beb90

Browse files
authored
Merge pull request #667 from ZJLLL114514/master
添加服务器AI聊天插件
2 parents 9a27ea2 + fef8eec commit 20beb90

File tree

7 files changed

+458
-0
lines changed

7 files changed

+458
-0
lines changed

Plugin.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeaponPlus", "src\WeaponPlu
266266
EndProject
267267
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dummy", "src\Dummy\Dummy.csproj", "{411A8F53-FEE2-4DC0-9965-7303FCBCB2F4}"
268268
EndProject
269+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AIChatPlugin", "src\AIChatPlugin\AIChatPlugin.csproj", "{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}"
270+
EndProject
269271
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SurvivalCrisis", "src\SurvivalCrisis\SurvivalCrisis.csproj", "{37365719-9365-4EF3-9B5F-1909797E142F}"
270272
EndProject
271273
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReverseWorld", "src\ReverseWorld\ReverseWorld.csproj", "{1E7334CF-E0C9-493D-8D6D-4E95BCB33E79}"
@@ -1310,6 +1312,14 @@ Global
13101312
{411A8F53-FEE2-4DC0-9965-7303FCBCB2F4}.Release|Any CPU.Build.0 = Release|Any CPU
13111313
{411A8F53-FEE2-4DC0-9965-7303FCBCB2F4}.Release|x64.ActiveCfg = Release|Any CPU
13121314
{411A8F53-FEE2-4DC0-9965-7303FCBCB2F4}.Release|x64.Build.0 = Release|Any CPU
1315+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1316+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Debug|Any CPU.Build.0 = Debug|Any CPU
1317+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Debug|x64.ActiveCfg = Debug|Any CPU
1318+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Debug|x64.Build.0 = Debug|Any CPU
1319+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Release|Any CPU.ActiveCfg = Release|Any CPU
1320+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Release|Any CPU.Build.0 = Release|Any CPU
1321+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Release|x64.ActiveCfg = Release|Any CPU
1322+
{A07C06F0-BE16-483B-BF8C-A3FEC49ADAC4}.Release|x64.Build.0 = Release|Any CPU
13131323
{37365719-9365-4EF3-9B5F-1909797E142F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13141324
{37365719-9365-4EF3-9B5F-1909797E142F}.Debug|Any CPU.Build.0 = Debug|Any CPU
13151325
{37365719-9365-4EF3-9B5F-1909797E142F}.Debug|x64.ActiveCfg = Debug|Any CPU

src/AIChatPlugin/AIChatPlugin.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using Terraria;
2+
using TerrariaApi.Server;
3+
using TShockAPI;
4+
using TShockAPI.Hooks;
5+
using System.Reflection;
6+
using static AIChatPlugin.Configuration;
7+
using static AIChatPlugin.Utils;
8+
9+
namespace AIChatPlugin;
10+
[ApiVersion(2, 1)]
11+
public class AIChatPlugin : TerrariaPlugin
12+
{
13+
#region 插件信息
14+
public override Version Version => new Version(2025, 1, 12);
15+
public override string Name => "AIChatPlugin";
16+
public override string Description => GetString("一个提供AI对话的插件");
17+
public override string Author => "忍";
18+
#endregion
19+
#region 插件启动
20+
public override void Initialize()
21+
{
22+
LoadConfig();
23+
Commands.ChatCommands.Add(new Command(this.ChatWithAICommand, "ab"));
24+
Commands.ChatCommands.Add(new Command("ailear", AIclear, "aiclear"));
25+
Commands.ChatCommands.Add(new Command(this.BotReset, "bcz"));
26+
Commands.ChatCommands.Add(new Command(this.BotHelp, "bbz"));
27+
GeneralHooks.ReloadEvent += this.GeneralHooks_ReloadEvent;
28+
PlayerHooks.PlayerLogout += this.OnPlayerLogout;
29+
}
30+
public AIChatPlugin(Main game) : base(game)
31+
{
32+
base.Order = 1;
33+
}
34+
#endregion
35+
#region 插件卸载
36+
protected override void Dispose(bool disposing)
37+
{
38+
if (disposing)
39+
{
40+
Commands.ChatCommands.RemoveAll(cmd => cmd.CommandDelegate.Method?.DeclaringType?.Assembly == Assembly.GetExecutingAssembly());
41+
GeneralHooks.ReloadEvent -= this.GeneralHooks_ReloadEvent;
42+
PlayerHooks.PlayerLogout -= this.OnPlayerLogout;
43+
}
44+
}
45+
#endregion
46+
#region 帮助信息
47+
private void BotHelp(CommandArgs args)
48+
{
49+
var helpMessage = " [i:1344]AIChatPlugin帮助信息[i:1344]\n" +
50+
"[i:1344]/ab - 向AI提问\n" +
51+
"[i:1344]/bcz - 清除您的上下文\n" +
52+
"[i:1344]/bbz - 显示此帮助信息\n" +
53+
"[i:1344]/aiclear - 清除所有人的上下文";
54+
args.Player.SendInfoMessage(helpMessage);
55+
}
56+
#endregion
57+
#region 读取配置
58+
private void GeneralHooks_ReloadEvent(ReloadEventArgs e)
59+
{
60+
LoadConfig();
61+
}
62+
#endregion
63+
#region 问题审核
64+
private void ChatWithAICommand(CommandArgs args)
65+
{
66+
if (args.Parameters.Count == 0)
67+
{
68+
args.Player.SendErrorMessage("[i:1344]请输入您想询问的内容![i:1344]");
69+
return;
70+
}
71+
var question = string.Join(" ", args.Parameters);
72+
ChatWithAI(args.Player, question);
73+
}
74+
#endregion
75+
#region 上文重置
76+
private void OnPlayerLogout(PlayerLogoutEventArgs e)
77+
{
78+
var playerId = e.Player.Index;
79+
if (playerContexts.ContainsKey(playerId))
80+
{
81+
playerContexts.Remove(playerId);
82+
}
83+
if (isProcessing.ContainsKey(playerId))
84+
{
85+
isProcessing.Remove(playerId);
86+
}
87+
}
88+
private void BotReset(CommandArgs args)
89+
{
90+
if (playerContexts.ContainsKey(args.Player.Index))
91+
{
92+
playerContexts.Remove(args.Player.Index);
93+
args.Player.SendSuccessMessage("[i:1344]您的上下文记录已重置![i:1344]");
94+
}
95+
else
96+
{
97+
args.Player.SendErrorMessage("[i:1344]您当前没有上下文记录![i:1344]");
98+
}
99+
}
100+
#endregion
101+
}

src/AIChatPlugin/AIChatPlugin.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="..\..\template.targets" />
3+
</Project>

src/AIChatPlugin/AIConfig.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Newtonsoft.Json;
2+
using TShockAPI;
3+
4+
namespace AIChatPlugin;
5+
public class Configuration
6+
{
7+
#region 创建配置
8+
[JsonProperty("回答字限制")] public int AIAnswerWordsLimit { get; set; } = 666;
9+
[JsonProperty("回答换行字")] public int AIAnswerWithLinebreaks { get; set; } = 50;
10+
[JsonProperty("上下文限制")] public int AIContextuallimitations { get; set; } = 10;
11+
[JsonProperty("超时时间")] public int AITimeoutPeriod { get; set; } = 100;
12+
[JsonProperty("名字")] public string AIName { get; set; } = "AI";
13+
[JsonProperty("设定")] public string AISettings { get; set; } = "你是一个简洁高效的AI,擅长用一句话精准概括复杂问题";
14+
public static readonly string FilePath = Path.Combine(TShock.SavePath, "AIChat.json");
15+
public static Configuration Config { get; private set; } = new Configuration();
16+
#endregion
17+
#region 读取配置
18+
public static void LoadConfig()
19+
{
20+
if (!File.Exists(FilePath) || new FileInfo(FilePath).Length == 0)
21+
{
22+
Config = new Configuration();
23+
var json = JsonConvert.SerializeObject(Config, Formatting.Indented);
24+
File.WriteAllText(FilePath, json);
25+
}
26+
else
27+
{
28+
try
29+
{
30+
var jsonContent = File.ReadAllText(FilePath);
31+
var tempConfig = JsonConvert.DeserializeObject<Configuration>(jsonContent) ?? new Configuration();
32+
Config = tempConfig;
33+
}
34+
catch (Exception ex)
35+
{
36+
TShock.Log.ConsoleError($"[AIChatPlugin] 加载配置时发生错误:{ex.Message}");
37+
}
38+
}
39+
}
40+
#endregion
41+
}

src/AIChatPlugin/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# AIChatPlugin AI聊天插件
2+
3+
- 它允许玩家通过聊天与一个 AI 对话系统进行互动
4+
- 该插件提供了一个简单的接口,使得玩家可以通过特定的命令或聊天触发词来向 AI 提出问题,并接收回答。
5+
6+
## 命令
7+
8+
| 指令 | 权限 | 作用 |
9+
| :-----| ----: | :----: |
10+
| /ab || 向 AI 提问 |
11+
| /bcz || 清除你的上下文记录 |
12+
| /bbz || 显示帮助信息 |
13+
| /aiclear | aiclear | 清除所有玩家的上下文记录 |
14+
| /reload | tshock.cfg.reload | 重载配置文件 |
15+
16+
## 配置文件
17+
18+
> 配置文件位于tshock/AIChat.json
19+
20+
```
21+
{
22+
"回答字限制": 666,
23+
"回答换行字": 50,
24+
"上下文限制": 10,
25+
"超时时间": 100,
26+
"名字": "AI",
27+
"设定": "你是一个简洁高效的AI,擅长用一句话精准概括复杂问题。",
28+
}
29+
```
30+
31+
## 注意事项
32+
33+
- 确保你的服务器网络连接正常,以便 AI 能够访问必要的 API。
34+
- AI无法回答敏感话题,否则会报错
35+
36+
## 更新日志
37+
38+
```
39+
40+
```
41+
42+
## 反馈
43+
- 优先发issued -> 共同维护的插件库:https://github.com/UnrealMultiple/TShockPlugin
44+
- 次优先:TShock官方群:816771079
45+
- 大概率看不到但是也可以:国内社区trhub.cn ,bbstr.net , tr.monika.love

0 commit comments

Comments
 (0)