157 lines
5.1 KiB
C#
157 lines
5.1 KiB
C#
using System.Text.Json;
|
|
using System.Xml;
|
|
using Lagrange.Core;
|
|
using Lagrange.Core.Common.Interface.Api;
|
|
using Lagrange.Core.Message;
|
|
using Shrink.Login;
|
|
|
|
namespace Shrink.Command;
|
|
|
|
public class Commands
|
|
{
|
|
private static Commands? _instance;
|
|
private static readonly object Lock = new();
|
|
private Commands() { }
|
|
|
|
public static Commands Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance != null) return _instance;
|
|
lock (Lock)
|
|
{
|
|
_instance ??= new Commands();
|
|
}
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
// 使用字典存储命令和群消息
|
|
private Dictionary<string, string> _commandDict = new();
|
|
private List<string> _corpus = new();
|
|
private Dictionary<uint, string> _messageDict = new();
|
|
private HashSet<uint> _adminSet = new();
|
|
|
|
// 文件初始化
|
|
public async Task Init()
|
|
{
|
|
// 读取Admins.json
|
|
if (File.Exists("Admins.json"))
|
|
{
|
|
_adminSet = new HashSet<uint>(JsonSerializer.Deserialize<List<uint>>(await File.ReadAllTextAsync("Admins.json")));
|
|
}
|
|
else
|
|
{
|
|
_adminSet.Add(3048536893); // 默认管理员
|
|
await File.WriteAllTextAsync("Admins.json", JsonSerializer.Serialize(_adminSet.ToList()));
|
|
}
|
|
|
|
// 读取JoinMessage.json
|
|
if (File.Exists("JoinMessage.json"))
|
|
{
|
|
_messageDict = JsonSerializer.Deserialize<List<KeyValuePair<uint, string>>>(await File.ReadAllTextAsync("JoinMessage.json"))
|
|
.ToDictionary(msg => msg.Key, msg => msg.Value);
|
|
}
|
|
else
|
|
{
|
|
_messageDict.Add(620902312, "欢迎");
|
|
await File.WriteAllTextAsync("JoinMessage.json", JsonSerializer.Serialize(_messageDict.Select(kv => new KeyValuePair<uint, string>(kv.Key, kv.Value)).ToList()));
|
|
}
|
|
|
|
// 读取Commands.json
|
|
if (File.Exists("Commands.json"))
|
|
{
|
|
_commandDict = JsonSerializer.Deserialize<List<KeyValuePair<string, string>>>(await File.ReadAllTextAsync("Commands.json"))
|
|
.ToDictionary(cmd => cmd.Key, cmd => cmd.Value);
|
|
}
|
|
else
|
|
{
|
|
_commandDict.Add("/ping", "Pong!");
|
|
await File.WriteAllTextAsync("Commands.json", JsonSerializer.Serialize(_commandDict.Select(kv => new KeyValuePair<string, string>(kv.Key, kv.Value)).ToList()));
|
|
}
|
|
}
|
|
|
|
private async Task SaveAdmin()
|
|
{
|
|
await File.WriteAllTextAsync("Admins.json", JsonSerializer.Serialize(_adminSet.ToList()));
|
|
await Init();
|
|
}
|
|
|
|
// bot管理员权限检查
|
|
private void NoEnoughPermission(BotContext ctx, uint groupID)
|
|
{
|
|
var chain = MessageBuilder.Group(groupID).Text("权限不足");
|
|
ctx.SendMessage(chain.Build());
|
|
}
|
|
|
|
// 先Init后再运行
|
|
public Task Run()
|
|
{
|
|
QrCode.Instance.Client.Invoker.OnGroupMemberIncreaseEvent += (context, @event) =>
|
|
{
|
|
var groupid = @event.GroupUin;
|
|
if (_messageDict.ContainsKey(groupid))
|
|
{
|
|
var message = _messageDict[groupid];
|
|
var chain = MessageBuilder.Group(groupid).Text(message);
|
|
context.SendMessage(chain.Build());
|
|
}
|
|
};
|
|
|
|
QrCode.Instance.Client.Invoker.OnGroupMessageReceived += (content, @event) =>
|
|
{
|
|
var groupId = @event.Chain.GroupUin.Value;
|
|
var senderId = @event.Chain.FriendUin;
|
|
var text = @event.Chain.ToPreviewText();
|
|
|
|
// 执行命令
|
|
if (_commandDict.ContainsKey(text))
|
|
{
|
|
var chain = MessageBuilder.Group(groupId).Text(_commandDict[text]);
|
|
content.SendMessage(chain.Build());
|
|
}
|
|
|
|
var today = DateTime.Today;
|
|
var seed = today.Year ^ today.Month ^ today.Day ^ senderId;
|
|
var random = new Random((int)seed);
|
|
|
|
switch (text)
|
|
{
|
|
case "/reload":
|
|
_ = Init();
|
|
var chain3 = MessageBuilder.Group(groupId).Text("重载完成!");
|
|
content.SendMessage(chain3.Build());
|
|
break;
|
|
}
|
|
|
|
|
|
if (text.Contains("/addadmin") && text.StartsWith("/addadmin"))
|
|
{
|
|
if (_adminSet.Contains(senderId))
|
|
{
|
|
var temp = uint.Parse(text.Remove(0, 9));
|
|
if (_adminSet.Contains(temp))
|
|
{
|
|
var chain = MessageBuilder.Group(groupId).Text("已存在管理员: " + temp);
|
|
content.SendMessage(chain.Build());
|
|
}
|
|
else
|
|
{
|
|
_adminSet.Add(temp);
|
|
var chain = MessageBuilder.Group(groupId).Text("已添加管理员: " + temp);
|
|
SaveAdmin();
|
|
content.SendMessage(chain.Build());
|
|
}
|
|
}
|
|
else
|
|
{
|
|
NoEnoughPermission(content, groupId);
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
return Task.CompletedTask;
|
|
}
|
|
}
|