Skip to content

Commit

Permalink
misc
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Nov 2, 2023
1 parent e9ed225 commit 59e2754
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.2.3.6</Version>
<Version>2.2.4.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
50 changes: 50 additions & 0 deletions XinjingdailyBot.Command/AdminCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ public async Task ResponseQueryBan(Message message, string[] args)
}
else
{
sb.AppendLine("投稿机器人封禁状态:");
var records = await _banRecordService.Queryable()
.Where(x => x.UserID == targetUser.UserID)
.OrderByDescending(static x => new { x.BanTime }).ToListAsync();
Expand Down Expand Up @@ -583,6 +584,19 @@ public async Task ResponseQueryBan(Message message, string[] args)
sb.AppendLine($"在 <code>{date}</code> 因为 <code>{record.Reason}</code> 被 <code>{admin}</code> {operate}");
}
}

sb.AppendLine();
sb.AppendLine("频道和群组封禁状态:");
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.AcceptChannel, targetUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.RejectChannel, targetUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.CommentGroup, targetUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.SubGroup, targetUser.UserID));
if (_channelService.HasSecondChannel)
{
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.SecondChannel, targetUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.SecondCommentGroup, targetUser.UserID));
}

}

await _botClient.SendCommandReply(sb.ToString(), message, false, parsemode: ParseMode.Html);
Expand Down Expand Up @@ -1696,4 +1710,40 @@ public async Task QResponseGotoLatest(Users dbUser, CallbackQuery callbackQuery)
await _botClient.AutoReplyAsync("今天没有未审核的稿件", callbackQuery, false);
}
}

//TODO
//[TextCmd("RECENTMESSAGE", EUserRights.AdminCmd, Description = "查询最近发言", Alias = "RECENTMSG")]
public async Task ResponseRecentMessage(Message message, string[] args)
{
async Task<string> exec()
{
var targetUser = await _userService.FetchTargetUser(message);

if (targetUser == null)
{
if (args.Any())
{
targetUser = await _userService.FetchUserByUserNameOrUserID(args.First());
}
}

if (targetUser == null)
{
return "找不到指定用户";
}

if (targetUser.Id == _channelService.BotUser.Id)
{
return "无法查询当前机器人的发言";
}

var sb = new StringBuilder();


return sb.ToString();
}

var text = await exec();
await _botClient.SendCommandReply(text, message, false, parsemode: ParseMode.Html);
}
}
22 changes: 20 additions & 2 deletions XinjingdailyBot.Command/CommonCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using XinjingdailyBot.Infrastructure.Attribute;
using XinjingdailyBot.Infrastructure.Enums;
using XinjingdailyBot.Infrastructure.Extensions;
using XinjingdailyBot.Interface.Bot.Common;
using XinjingdailyBot.Interface.Bot.Handler;
using XinjingdailyBot.Interface.Data;
using XinjingdailyBot.Interface.Helper;
Expand All @@ -25,19 +26,22 @@ internal class CommonCommand
private readonly IBanRecordService _banRecordService;
private readonly ITextHelperService _textHelperService;
private readonly ICommandHandler _commandHandler;
private readonly IChannelService _channelService;

public CommonCommand(
ITelegramBotClient botClient,
IOptions<OptionsSetting> options,
IBanRecordService banRecordService,
ITextHelperService textHelperService,
ICommandHandler commandHandler)
ICommandHandler commandHandler,
IChannelService channelService)
{
_botClient = botClient;
_optionsSetting = options.Value;
_banRecordService = banRecordService;
_textHelperService = textHelperService;
_commandHandler = commandHandler;
_channelService = channelService;
}

/// <inheritdoc cref="IBanRecordService.WarnDuration"/>
Expand Down Expand Up @@ -151,6 +155,7 @@ public async Task ResponseMyBan(Users dbUser, Message message)

var sb = new StringBuilder();

sb.AppendLine("投稿机器人封禁状态:");
string status = dbUser.IsBan ? "已封禁" : "正常";
sb.AppendLine($"用户名: <code>{dbUser.EscapedFullName()}</code>");
sb.AppendLine($"用户ID: <code>{dbUser.UserID}</code>");
Expand Down Expand Up @@ -180,7 +185,7 @@ public async Task ResponseMyBan(Users dbUser, Message message)
EBanType.GlobalUnBan => "撤销全局封禁",
_ => "其他",
};
sb.AppendLine($"在 <code>{date}</code> 因为 <code>{record.Reason}</code> 被{operate}");
sb.AppendLine($"在 <code>{date}</code> 因为 <code>{record.Reason}</code> 被 {operate}");
if (record.Type == EBanType.UnBan || record.Type == EBanType.Ban)
{
sb.AppendLine();
Expand All @@ -189,6 +194,19 @@ public async Task ResponseMyBan(Users dbUser, Message message)
}
sb.AppendLine("\n仅显示90天内的警告记录");

sb.AppendLine();
sb.AppendLine("频道和群组封禁状态:");
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.AcceptChannel, dbUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.RejectChannel, dbUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.CommentGroup, dbUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.SubGroup, dbUser.UserID));

if (_channelService.HasSecondChannel)
{
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.SecondChannel, dbUser.UserID));
sb.AppendLine(await _botClient.GetChatMemberStatusAsync(_channelService.SecondCommentGroup, dbUser.UserID));
}

await _botClient.SendCommandReply(sb.ToString(), message, parsemode: ParseMode.Html);
}
}
29 changes: 12 additions & 17 deletions XinjingdailyBot.Command/NormalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,14 @@ public async Task ResponseMyRight(Users dbUser, Message message)
[TextCmd("ADMIN", EUserRights.NormalCmd, Description = "艾特群管理")]
public async Task ResponseCallAdmins(Users dbUser, Message message)
{
var sb = new StringBuilder();

if (message.Chat.Type != ChatType.Group && message.Chat.Type != ChatType.Supergroup)
{
sb.AppendLine("该命令仅在群组内有效");
await _botClient.SendCommandReply("该命令仅在群组内有效", message, false, ParseMode.Html);
}
else
{
var sb = new StringBuilder();
var admins = await _botClient.GetChatAdministratorsAsync(message.Chat.Id);

foreach (var menber in admins)
{
var admin = menber.User;
Expand All @@ -207,21 +205,18 @@ public async Task ResponseCallAdmins(Users dbUser, Message message)
sb.AppendLine($"@{admin.Username}");
}
}
}

sb.AppendLine($"用户 {dbUser} 呼叫群管理");
var msg = await _botClient.SendCommandReply(sb.ToString(), message, false, ParseMode.Html);
await Task.Delay(2000);

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

Expand Down
52 changes: 52 additions & 0 deletions XinjingdailyBot.Infrastructure/Extensions/BotClientExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ public static async Task<Message> SendCommandReply(
return msg;
}

/// <summary>
/// 发送会话状态
/// </summary>
/// <param name="botClient"></param>
/// <param name="message"></param>
/// <param name="chatAction"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public static Task SendChatActionAsync(
this ITelegramBotClient botClient,
Message message,
Expand All @@ -165,4 +173,48 @@ public static Task SendChatActionAsync(
return Task.CompletedTask;
}
}

/// <summary>
/// 获取群成员状态
/// </summary>
/// <param name="botClient"></param>
/// <param name="chat"></param>
/// <param name="userId"></param>
/// <returns></returns>
public static async Task<string> GetChatMemberStatusAsync(
this ITelegramBotClient botClient,
Chat? chat,
long userId)
{
string title = chat?.Title?.EscapeHtml() ?? "未设置群组";

string status;
if (chat != null)
{
try
{
var chatMember = await botClient.GetChatMemberAsync(chat, userId);
status = chatMember.Status switch {
ChatMemberStatus.Creator or
ChatMemberStatus.Administrator or
ChatMemberStatus.Member or
ChatMemberStatus.Left => "正常",
ChatMemberStatus.Kicked => "封禁",
ChatMemberStatus.Restricted => "受限",
_ => "未知",
};
}
catch (Exception ex)
{
_logger.Error(ex, "读取用户状态出错");
status = "出错";
}
}
else
{
status = "---";
}

return string.Format("{0}: {1}", title, status);
}
}
3 changes: 3 additions & 0 deletions XinjingdailyBot.Interface/Bot/Common/IChannelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public interface IChannelService
/// 第二频道
/// </summary>
Chat? SecondChannel { get; }
/// <summary>
/// 第二频道评论区
/// </summary>
Chat? SecondCommentGroup { get; }
/// <summary>
/// 拒绝频道
Expand Down
4 changes: 4 additions & 0 deletions XinjingdailyBot.Interface/Bot/Handler/ICommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ public interface ICommandHandler
/// <param name="query"></param>
/// <returns></returns>
Task OnQueryCommandReceived(Users dbUser, CallbackQuery query);
/// <summary>
/// 清除机器人命令
/// </summary>
/// <returns></returns>
Task<bool> ClearCommandsMenu();
}
4 changes: 4 additions & 0 deletions XinjingdailyBot.Interface/Data/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public interface IPostService : IBaseService<NewPosts>
/// <param name="message"></param>
/// <returns></returns>
Task<NewPosts?> FetchPostFromReplyToMessage(Message message);
/// <summary>
/// 获取最新未审核稿件
/// </summary>
/// <returns></returns>
Task<NewPosts?> GetLatestReviewingPostLink();

/// <summary>
Expand Down
24 changes: 23 additions & 1 deletion XinjingdailyBot.Interface/Data/IReviewStatusService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
using Telegram.Bot.Types;
using XinjingdailyBot.Interface.Data.Base;
using XinjingdailyBot.Model.Models;

namespace XinjingdailyBot.Interface.Data;
public interface IReviewStatusService
/// <summary>
/// 审核状态服务
/// </summary>
public interface IReviewStatusService : IBaseService<ReviewStatus>
{
/// <summary>
/// 创建新记录
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
Task CreateNewReviewStatus(Message message);
/// <summary>
/// 删除旧记录
/// </summary>
/// <returns></returns>
Task DeleteOldReviewStatus();
/// <summary>
/// 删除记录
/// </summary>
/// <param name="reviewStatus"></param>
/// <returns></returns>
Task DeleteReviewStatus(ReviewStatus reviewStatus);
/// <summary>
/// 获取旧的审核状态
/// </summary>
/// <returns></returns>
Task<ReviewStatus?> GetOldReviewStatu();
}
7 changes: 6 additions & 1 deletion XinjingdailyBot.Interface/Helper/IMarkupHelperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ public interface IMarkupHelperService
/// </summary>
/// <returns></returns>
InlineKeyboardMarkup ReviewKeyboardB();
InlineKeyboardMarkup ReviewStatusButton();
/// <summary>
/// 获取指向今日最早未审核稿件的链接
/// </summary>
/// <param name="post"></param>
/// <returns></returns>
InlineKeyboardMarkup ReviewStatusButton(NewPosts? post);

/// <summary>
/// 频道选项键盘
Expand Down
4 changes: 2 additions & 2 deletions XinjingdailyBot.Model/Models/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public override string ToString()
{
if (string.IsNullOrEmpty(UserName))
{
return $"{FullName}(#{UserID})";
return $"{FullName}(#{UserID})".EscapeHtml();
}
else
{
return $"{FullName}(@{UserName})";
return $"{FullName}(@{UserName})".EscapeHtml();
}
}

Expand Down
30 changes: 23 additions & 7 deletions XinjingdailyBot.Service/Helper/MarkupHelperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,16 +539,32 @@ public InlineKeyboardMarkup NukeMenuKeyboard(Users dbUser, Users targetUser, str
return line.Any() ? new InlineKeyboardMarkup(new[] { line }) : null;
}

public InlineKeyboardMarkup ReviewStatusButton()
public InlineKeyboardMarkup ReviewStatusButton(NewPosts? post)
{
var keyboard = new InlineKeyboardMarkup(new[]
InlineKeyboardMarkup keyboard;
if (post != null)
{
new[]
{
InlineKeyboardButton.WithCallbackData("今日最早未审核稿件", $"cmd -1 gotolatest"),
},
});
var chatId = Math.Abs(post.ReviewChatID + 1000000000000);
var link = $"https://t.me/c/{chatId}/{post.ReviewMsgID}";

keyboard = new InlineKeyboardMarkup(new[]
{
new[]
{
InlineKeyboardButton.WithUrl($"前往稿件 {post.Id}", link),
},
});
}
else
{
keyboard = new InlineKeyboardMarkup(new[]
{
new[]
{
InlineKeyboardButton.WithCallbackData("无待审核稿件", $"cmd -1 say 无待审核稿件"),
},
});
}
return keyboard;
}
}
Loading

0 comments on commit 59e2754

Please sign in to comment.