Skip to content

Commit

Permalink
feat 修改日志
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Oct 18, 2023
1 parent 254dba2 commit f73ae91
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
21 changes: 19 additions & 2 deletions XinjingdailyBot.Command/NormalCommand.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using SqlSugar;
using System.Net;
using System.Text;
Expand All @@ -20,6 +21,7 @@ namespace XinjingdailyBot.Command;
[AppService(LifeTime.Scoped)]
internal class NormalCommand
{
private readonly ILogger<NormalCommand> _logger;
private readonly ITelegramBotClient _botClient;
private readonly IUserService _userService;
private readonly GroupRepository _groupRepository;
Expand All @@ -31,6 +33,7 @@ internal class NormalCommand
private readonly IMediaGroupService _mediaGroupService;

public NormalCommand(
ILogger<NormalCommand> logger,
ITelegramBotClient botClient,
IUserService userService,
GroupRepository groupRepository,
Expand All @@ -41,6 +44,7 @@ public NormalCommand(
IHttpHelperService httpHelperService,
IMediaGroupService mediaGroupService)
{
_logger = logger;
_botClient = botClient;
_userService = userService;
_groupRepository = groupRepository;
Expand Down Expand Up @@ -182,7 +186,7 @@ public async Task ResponseMyRight(Users dbUser, Message message)
/// <param name="message"></param>
/// <returns></returns>
[TextCmd("ADMIN", EUserRights.NormalCmd, Description = "艾特群管理")]
public async Task ResponseCallAdmins(Message message)
public async Task ResponseCallAdmins(Users dbUser, Message message)
{
var sb = new StringBuilder();

Expand All @@ -204,7 +208,20 @@ public async Task ResponseCallAdmins(Message message)
}
}

await _botClient.SendCommandReply(sb.ToString(), message);
sb.AppendLine($"用户 {dbUser} 呼叫群管理");

var msg = await _botClient.SendCommandReply(sb.ToString(), message, false, ParseMode.Html);

await Task.Delay(2000);

try
{
await _botClient.EditMessageTextAsync(msg, $"用户 {dbUser} 呼叫群管理", ParseMode.Html);
}
catch (Exception)
{
_logger.LogWarning("编辑消息失败");
}
}

/// <summary>
Expand Down
66 changes: 58 additions & 8 deletions XinjingdailyBot.Infrastructure/Extensions/LoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using System;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;

Expand Down Expand Up @@ -36,6 +37,9 @@ public static void LogUpdate(this ILogger logger, Update update)
case UpdateType.InlineQuery:
logger.LogInlineQuery(update.InlineQuery!);
break;
case UpdateType.MyChatMember:
logger.LogMyChatMember(update.MyChatMember!);
break;
default:
logger.LogDebug("U 未知消息 {Type}", update.Type);
break;
Expand Down Expand Up @@ -72,12 +76,12 @@ public static void LogMessage(this ILogger logger, Message message)
var chat = message.Chat;

string chatFrom = chat.Type switch {
ChatType.Private => $"私聊|{chat.FirstName}{chat.LastName}",
ChatType.Group => $"群组|{chat.Title}",
ChatType.Channel => $"频道|{chat.Title}",
ChatType.Supergroup => $"群组|{chat.Title}",
ChatType.Sender => $"发送者|{chat.Title}",
_ => $"未知|{chat.Title}",
ChatType.Private => $"[私聊|{chat.FirstName}{chat.LastName}]",
ChatType.Group => $"[群组|{chat.Title}]",
ChatType.Channel => $"[频道|{chat.Title}]",
ChatType.Supergroup => $"[群组|{chat.Title}]",
ChatType.Sender => $"[发送者|{chat.Title}]",
_ => $"[未知|{chat.Title}]",
};

string user = message.From?.UserToString() ?? "未知";
Expand All @@ -93,7 +97,7 @@ public static void LogMessage(this ILogger logger, Message message)
public static void LogCallbackQuery(this ILogger logger, CallbackQuery callbackQuery)
{
string user = callbackQuery.From.UserToString();
logger.LogInformation("回调|{Id} {user} {Data}", callbackQuery.Id, user, callbackQuery.Data);
logger.LogInformation("[回调|{Id}] {user} {Data}", callbackQuery.Id, user, callbackQuery.Data);
}

/// <summary>
Expand All @@ -104,6 +108,52 @@ public static void LogCallbackQuery(this ILogger logger, CallbackQuery callbackQ
public static void LogInlineQuery(this ILogger logger, InlineQuery inlineQuery)
{
string user = inlineQuery.From.UserToString();
logger.LogDebug("【查询|{Id}】 {user} {Data}", inlineQuery.Id, user, inlineQuery.Query);
logger.LogDebug("[查询|{Id}] {user} {Data}", inlineQuery.Id, user, inlineQuery.Query);
}

/// <summary>
/// 输出MyChatMember
/// </summary>
/// <param name="logger"></param>
/// <param name="chatMemberUpdated"></param>
public static void LogMyChatMember(this ILogger logger, ChatMemberUpdated chatMemberUpdated)
{
var chat = chatMemberUpdated.Chat;
string chatFrom = chat.Type switch {
ChatType.Private => $"[私聊|{chat.FirstName}{chat.LastName}]",
ChatType.Group => $"[群组|{chat.Title}]",
ChatType.Channel => $"[频道|{chat.Title}]",
ChatType.Supergroup => $"[群组|{chat.Title}]",
ChatType.Sender => $"[发送者|{chat.Title}]",
_ => $"[未知|{chat.Title}]",
};

var op = chatMemberUpdated.From.UserToString();
var user = chatMemberUpdated.NewChatMember.User.UserToString();

var oldAction = chatMemberUpdated.OldChatMember.GetMemberAction();
var newAction = chatMemberUpdated.NewChatMember.GetMemberAction();

if (op != user)
{
logger.LogDebug("[成员变更] {chatFrom} 用户 {op} 设置 {user} {old}->{new}", chatFrom, op, user, oldAction, newAction);
}
else
{
logger.LogDebug("[成员变更] {chatFrom} 用户 {user} {old}->{new}", chatFrom, user, oldAction, newAction);
}
}

private static string GetMemberAction(this ChatMember chatMember)
{
return chatMember.Status switch {
ChatMemberStatus.Creator => "所有者",
ChatMemberStatus.Administrator => "管理员",
ChatMemberStatus.Member => "普通成员",
ChatMemberStatus.Left => "退出",
ChatMemberStatus.Kicked => "被踢出",
ChatMemberStatus.Restricted => "被限制",
_ => "未知",
};
}
}

0 comments on commit f73ae91

Please sign in to comment.