Skip to content

Commit

Permalink
修正 /help 命令报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jan 7, 2024
1 parent 2a6e954 commit b9d74dd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 114 deletions.
103 changes: 0 additions & 103 deletions XinjingdailyBot.Command/ObsoleteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,107 +319,4 @@ public async Task ResponseFixPost(Message message)
await _botClient.SendCommandReply($"修补稿件表完成, 更新了 {effectCount} 条记录", message, autoDelete: false);
}
}

/// <summary>
/// 自动升级机器人
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
[TextCmd("UPDATE", EUserRights.SuperCmd, Description = "自动升级机器人")]
public async Task ResponseUpdate(Message message)
{
async Task<string> exec()
{
if (!BuildInfo.CanUpdate)
{
return "当前版本不支持自动升级";
}

var releaseResponse = await _httpHelperService.GetLatestRelease();

if (releaseResponse == null)
{
return "读取在线版本信息失败";
}

if (Utils.Version == releaseResponse.TagName)
{
return string.Format("当前已经是最新版本 {0}", Utils.Version);
}

string varint = BuildInfo.Variant;
string? downloadUrl = null;

foreach (var asset in releaseResponse.Assets)
{
if (asset.Name.Contains(varint))
{
downloadUrl = asset.DownloadUrl;
break;
}
}

if (string.IsNullOrEmpty(downloadUrl) && releaseResponse.Assets.Count != 0)
{
return "自动更新失败, 找不到适配的更新包";
}

var bs = await _httpHelperService.DownloadRelease(downloadUrl).ConfigureAwait(false);

if (bs == null)
{
return "自动更新失败, 下载更新包失败";
}

try
{
await using (bs.ConfigureAwait(false))
{
using var zipArchive = new ZipArchive(bs);

string currentPath = Utils.ExeFullPath;
string backupPath = Utils.BackupFullPath;

System.IO.File.Move(currentPath, backupPath, true);

int count = 0;
foreach (var entry in zipArchive.Entries)
{
if (entry.FullName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)
|| entry.FullName.Equals(Utils.ExeFileName, StringComparison.OrdinalIgnoreCase))
{
entry.ExtractToFile(currentPath);
count++;
}
}

if (count > 0)
{
var sb = new StringBuilder();
sb.AppendLine(string.Format("自动更新成功, 更新了{0}个文件", count));
sb.AppendLine(string.Format("版本变动: {0} -> {1}", Utils.Version, releaseResponse.TagName));
sb.AppendLine();
sb.AppendLine("发行版日志:");
sb.AppendLine(string.Format("<code>{0}</code>", releaseResponse.Body));
sb.AppendLine();
sb.AppendLine(_textHelperService.HtmlLink(releaseResponse.Url, "在线查看"));
return sb.ToString();
}
else
{
System.IO.File.Move(backupPath, currentPath);
return "自动更新失败, 无文件变动";
}
}
}
catch (Exception ex)
{
_logger.LogError(ex, "自动更新失败, 解压遇到问题");
return "自动更新失败, 解压遇到问题";
}
}

string text = await exec();
await _botClient.SendCommandReply(text, message, false, ParseMode.Html);
}
}
39 changes: 28 additions & 11 deletions XinjingdailyBot.Service/Bot/Handler/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,22 @@ public CommandHandler(
/// <summary>
/// 指令方法名映射
/// </summary>
private readonly Dictionary<Type, Dictionary<string, AssemblyMethod>> _commandClass = new();
private readonly Dictionary<Type, Dictionary<string, AssemblyMethod>> _commandClass = [];
/// <summary>
/// 指令别名
/// </summary>
private readonly Dictionary<Type, Dictionary<string, string>> _commandAlias = new();
private readonly Dictionary<Type, Dictionary<string, string>> _commandAlias = [];

/// <summary>
/// Query指令方法名映射
/// </summary>
private readonly Dictionary<Type, Dictionary<string, AssemblyMethod>> _queryCommandClass = new();
private readonly Dictionary<Type, Dictionary<string, AssemblyMethod>> _queryCommandClass = [];
/// <summary>
/// Query指令别名
/// </summary>
private readonly Dictionary<Type, Dictionary<string, string>> _queryCommandAlias = new();
private readonly Dictionary<Type, Dictionary<string, string>> _queryCommandAlias = [];

private static readonly char[] separator = [','];

[RequiresUnreferencedCode("不兼容剪裁")]
public void InstallCommands()
Expand Down Expand Up @@ -101,15 +103,21 @@ private void RegisterCommands([DynamicallyAccessedMembers(DynamicallyAccessedMem
var alias = textAttribute.Alias?.ToUpperInvariant();
var description = textAttribute.Description;
var rights = textAttribute.Rights;
commands.Add(command, new AssemblyMethod(method, description, rights));
if (!commands.TryAdd(command, new AssemblyMethod(method, description, rights)))
{
_logger.LogWarning("注册命令 {cmd} 失败, 命令名称重复", command);
}

//添加别名
if (!string.IsNullOrEmpty(alias))
{
var splitedAlias = alias.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var splitedAlias = alias.Split(separator, StringSplitOptions.RemoveEmptyEntries);
foreach (var split in splitedAlias)
{
commandAlias.Add(split, command);
if (!commandAlias.TryAdd(split, command))
{
_logger.LogWarning("注册命令 {cmd} 别名 {alias} 失败, 命令别名重复", command, split);
}
}
}
}
Expand All @@ -123,15 +131,21 @@ private void RegisterCommands([DynamicallyAccessedMembers(DynamicallyAccessedMem
var alias = queryAttribute.Alias?.ToUpperInvariant();
var description = queryAttribute.Description;
var rights = queryAttribute.Rights;
queryCommands.Add(command, new AssemblyMethod(method, description, rights));
if (!queryCommands.TryAdd(command, new AssemblyMethod(method, description, rights)))
{
_logger.LogWarning("注册Query命令 {cmd} 失败, 命令名称重复", command);
}

//添加别名
if (!string.IsNullOrEmpty(alias))
{
var splitedAlias = alias.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var splitedAlias = alias.Split(separator, StringSplitOptions.RemoveEmptyEntries);
foreach (var split in splitedAlias)
{
queryAlias.Add(split, command);
if (!queryAlias.TryAdd(split, command))
{
_logger.LogWarning("注册Query命令 {cmd} 别名 {split} 失败, 命令别名重复", command, split);
}
}
}
}
Expand Down Expand Up @@ -430,7 +444,10 @@ public string GetAvilabeCommands(Users dbUser)
{
if (!string.IsNullOrEmpty(method.Description))
{
cmds.Add(cmd.ToLowerInvariant(), method.Description);
if (!cmds.TryAdd(cmd.ToLowerInvariant(), method.Description))
{
_logger.LogWarning("命令 {cmd} 重复, 请检查代码逻辑", cmd);
}
}
}
}
Expand Down

0 comments on commit b9d74dd

Please sign in to comment.