Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Sep 7, 2023
1 parent e04a2ba commit fdc8741
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 32 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.1.6.0</Version>
<Version>2.2.0.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
10 changes: 5 additions & 5 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project>
<ItemGroup>
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9" />
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.10" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageVersion Include="MySql.Data" Version="8.1.0" />
<PackageVersion Include="NLog" Version="5.2.3" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.2" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="5.3.2" />
<PackageVersion Include="NLog" Version="5.2.4" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.4" />
<PackageVersion Include="NLog.Web.AspNetCore" Version="5.3.4" />
<PackageVersion Include="Quartz" Version="3.7.0" />
<PackageVersion Include="Quartz.AspNetCore" Version="3.7.0" />
<PackageVersion Include="SqlSugar.IOC" Version="2.0.0" />
<PackageVersion Include="SqlSugarCore" Version="5.1.4.94" />
<PackageVersion Include="SqlSugarCore" Version="5.1.4.105" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.Annotations" Version="6.5.0" />
<PackageVersion Include="Telegram.Bot" Version="19.0.0" />
Expand Down
169 changes: 159 additions & 10 deletions XinjingdailyBot.Command/AdminCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ internal class AdminCommand
private readonly IMarkupHelperService _markupHelperService;
private readonly ICmdRecordService _cmdRecordService;
private readonly IUserTokenService _userTokenService;
private readonly IMediaGroupService _mediaGroupService;

public AdminCommand(
ILogger<AdminCommand> logger,
Expand All @@ -51,7 +52,8 @@ public AdminCommand(
IChannelService channelService,
IMarkupHelperService markupHelperService,
ICmdRecordService cmdRecordService,
IUserTokenService userTokenService)
IUserTokenService userTokenService,
IMediaGroupService mediaGroupService)
{
_logger = logger;
_botClient = botClient;
Expand All @@ -65,6 +67,7 @@ public AdminCommand(
_markupHelperService = markupHelperService;
_cmdRecordService = cmdRecordService;
_userTokenService = userTokenService;
_mediaGroupService = mediaGroupService;
}

/// <summary>
Expand All @@ -73,10 +76,10 @@ public AdminCommand(
private readonly DateTime StartAt = DateTime.Now;

/// <inheritdoc cref="IBanRecordService.WarningLimit"/>
private static int WarningLimit = IBanRecordService.WarningLimit;
private readonly int WarningLimit = IBanRecordService.WarningLimit;

/// <inheritdoc cref="IBanRecordService.WarnDuration"/>
private static int WarnDuration = IBanRecordService.WarnDuration;
private readonly int WarnDuration = IBanRecordService.WarnDuration;

/// <summary>
/// 获取群组信息
Expand Down Expand Up @@ -551,7 +554,7 @@ public async Task ResponseQueryBan(Message message, string[] args)

foreach (var record in records)
{
var date = record.BanTime.ToString("d");
var date = record.BanTime.ToString("yyyy-MM-dd HH:mm:ss");
var operate = record.Type switch {
EBanType.UnBan => "解封",
EBanType.Ban => "封禁",
Expand Down Expand Up @@ -809,6 +812,8 @@ public async Task ResponsePostReport(Message message)
var monthStart = now.AddDays(1 - now.Day).AddHours(-now.Hour).AddMinutes(-now.Minute).AddSeconds(-now.Second);
var yearStart = now.AddMonths(1 - now.Month).AddDays(1 - now.Day).AddHours(-now.Hour).AddMinutes(-now.Minute).AddSeconds(-now.Second);

var second = _channelService.HasSecondChannel;

var sb = new StringBuilder();

var todayPost = await _postService.Queryable().Where(x => x.CreateAt >= prev1Day && x.Status > EPostStatus.Cancel).CountAsync();
Expand All @@ -818,6 +823,11 @@ public async Task ResponsePostReport(Message message)

sb.AppendLine("-- 24小时投稿统计 --");
sb.AppendLine($"接受/拒绝: <code>{todayAcceptPost}</code> / <code>{todayRejectPost}</code>");
if (second)
{
var todayAcceptSecondPost = await _postService.Queryable().Where(x => x.CreateAt >= prev1Day && x.Status == EPostStatus.AcceptedSecond).CountAsync();
sb.AppendLine($"接受(二频): <code>{todayAcceptSecondPost}</code>");
}
sb.AppendLine($"通过率: <code>{(todayPost > 0 ? (100 * todayAcceptPost / todayPost).ToString("f2") : "--")}%</code>");
sb.AppendLine($"过期投稿: <code>{todayExpiredPost}</code>");
sb.AppendLine($"累计投稿: <code>{todayPost + todayExpiredPost}</code>");
Expand All @@ -830,6 +840,11 @@ public async Task ResponsePostReport(Message message)
sb.AppendLine();
sb.AppendLine("-- 7日投稿统计 --");
sb.AppendLine($"接受/拒绝: <code>{weekAcceptPost}</code> / <code>{weekRejectPost}</code>");
if (second)
{
var weekAcceptSecondPost = await _postService.Queryable().Where(x => x.CreateAt >= prev7Days && x.Status == EPostStatus.AcceptedSecond).CountAsync();
sb.AppendLine($"接受(二频): <code>{weekAcceptSecondPost}</code>");
}
sb.AppendLine($"通过率: <code>{(weekPost > 0 ? (100 * weekAcceptPost / weekPost).ToString("f2") : "--")}%</code>");
sb.AppendLine($"过期投稿: <code>{weekExpiredPost}</code>");
sb.AppendLine($"累计投稿: <code>{weekPost + weekExpiredPost}</code>");
Expand All @@ -842,6 +857,11 @@ public async Task ResponsePostReport(Message message)
sb.AppendLine();
sb.AppendLine($"-- {monthStart.ToString("MM")}月投稿统计 --");
sb.AppendLine($"接受/拒绝: <code>{monthAcceptPost}</code> / <code>{monthRejectPost}</code>");
if (second)
{
var monthAcceptSecondPost = await _postService.Queryable().Where(x => x.CreateAt >= monthStart && x.Status == EPostStatus.AcceptedSecond).CountAsync();
sb.AppendLine($"接受(二频): <code>{monthAcceptSecondPost}</code>");
}
sb.AppendLine($"通过率: <code>{(monthPost > 0 ? (100 * monthAcceptPost / monthPost).ToString("f2") : "--")}%</code>");
sb.AppendLine($"过期投稿: <code>{monthExpiredPost}</code>");
sb.AppendLine($"累计投稿: <code>{monthPost}</code>");
Expand All @@ -854,6 +874,11 @@ public async Task ResponsePostReport(Message message)
sb.AppendLine();
sb.AppendLine($"-- {yearStart.ToString("yyyy")}年投稿统计 --");
sb.AppendLine($"接受/拒绝: <code>{yearAcceptPost}</code> / <code>{yearRejectPost}</code>");
if (second)
{
var yearAcceptSecondPost = await _postService.Queryable().Where(x => x.CreateAt >= yearStart && x.Status == EPostStatus.AcceptedSecond).CountAsync();
sb.AppendLine($"接受(二频): <code>{yearAcceptSecondPost}</code>");
}
sb.AppendLine($"通过率: <code>{(yearPost > 0 ? (100 * yearAcceptPost / yearPost).ToString("f2") : "--")}%</code>");
sb.AppendLine($"过期投稿: <code>{yearExpiredPost}</code>");
sb.AppendLine($"累计投稿: <code>{yearPost}</code>");
Expand All @@ -868,6 +893,11 @@ public async Task ResponsePostReport(Message message)
sb.AppendLine();
sb.AppendLine("-- 历史投稿统计 --");
sb.AppendLine($"接受/拒绝: <code>{totalAcceptPost}</code> / <code>{totalRejectPost}</code>");
if (second)
{
var totalAcceptSecondPost = await _postService.Queryable().Where(static x => x.Status == EPostStatus.AcceptedSecond).CountAsync();
sb.AppendLine($"接受(二频): <code>{totalAcceptSecondPost}</code>");
}
sb.AppendLine($"通过率: <code>{(totalPost > 0 ? (100 * totalAcceptPost / totalPost).ToString("f2") : "--")}%</code>");
sb.AppendLine($"过期投稿: <code>{totalExpiredPost}</code>");
sb.AppendLine($"累计投稿: <code>{totalPost}</code>");
Expand Down Expand Up @@ -922,8 +952,8 @@ public async Task ResponseSystemReport(Message message)

sb.AppendLine();
sb.AppendLine("-- 调用统计 --");
sb.AppendLine($"文字命令: <code>{cmdCount}</code> 次");
sb.AppendLine($"查询调用: <code>{QueryCount}</code> 次");
sb.AppendLine($"Text命令: <code>{cmdCount}</code> 次");
sb.AppendLine($"Query命令: <code>{QueryCount}</code> 次");
sb.AppendLine($"出错次数: <code>{errorCount}</code> 次");

sb.AppendLine();
Expand Down Expand Up @@ -1478,17 +1508,136 @@ public async Task ResponseToken(Users dbUser, Message message)
}
}

/// <summary>
/// 撤回稿件
/// </summary>
/// <param name="dbUser"></param>
/// <param name="callbackQuery"></param>
/// <param name="args"></param>
/// <returns></returns>
[QueryCmd("DELETEPOST", EUserRights.AdminCmd)]
public async Task QDeletePost(CallbackQuery callbackQuery, string[] args)
{
async Task<string> exec()
{
if (args.Length < 2)
{
return "参数有误";
}

NewPosts? post = null;

if (int.TryParse(args[1], out int postId))
{
post = await _postService.Queryable().FirstAsync(x => x.Id == postId);
}

if (post == null)
{
return "未找到稿件";
}

if (post.Status != EPostStatus.Accepted && post.Status != EPostStatus.AcceptedSecond)
{
return "稿件可能已被撤回";
}


if (post.IsMediaGroup)
{
var mediaGroups = await _mediaGroupService.QueryMediaGroup(post.PublishMediaGroupID);
int error = 0;
if (!mediaGroups.Any())
{
return "找不到媒体组信息";
}
foreach (var group in mediaGroups)
{
try
{
await _botClient.DeleteMessageAsync(group.ChatID, (int)group.MessageID);
}
catch (Exception ex)
{
_logger.LogError(ex, "删除消息失败");
error++;
}
}

return $"媒体组稿件撤回完成, 成功删除 {mediaGroups.Count - error} 条消息";
}
else
{
var chat = post.Status == EPostStatus.Accepted ? _channelService.AcceptChannel : _channelService.SecondChannel;
if (chat == null)
{
return "第二频道未设定";
}

try
{
await _botClient.DeleteMessageAsync(chat, (int)post.PublicMsgID);
return "删除消息成功";
}
catch (Exception ex)
{
_logger.LogError(ex, "删除消息失败");
return "删除消息失败";
}
}
}
string text = await exec();
await _botClient.AutoReplyAsync(text, callbackQuery.Message!, ParseMode.Html);
await _botClient.EditMessageReplyMarkupAsync(callbackQuery.Message!, null);
}

[QueryCmd("QUERYPOSTER", EUserRights.AdminCmd)]
public async Task QQueryPoster(CallbackQuery callbackQuery, string[] args)
{
async Task<string> exec()
{
if (args.Length < 2)
{
return "参数有误";
}

Users? user = null;

if (int.TryParse(args[1], out int userId))
{
user = await _userService.Queryable().FirstAsync(x => x.UserID == userId);
}

if (user == null)
{
return "找不到用户";
}

var sb = new StringBuilder();

sb.AppendLine("-- 基础信息 --");
sb.AppendLine(_userService.GetUserBasicInfo(user));

sb.AppendLine();
sb.AppendLine("-- 用户排名 --");
sb.AppendLine(await _userService.GetUserRank(user));

return sb.ToString();
}
string text = await exec();
await _botClient.AutoReplyAsync(text, callbackQuery.Message!, ParseMode.Html);
}

/// <summary>
/// 补发稿件
/// </summary>
/// <param name="dbUser"></param>
/// <param name="query"></param>
/// <param name="callbackQuery"></param>
/// <returns></returns>
[QueryCmd("REPOST", EUserRights.AdminCmd)]
public async Task QResponseRepost(Users dbUser, CallbackQuery query)
public async Task QResponseRepost(Users dbUser, CallbackQuery callbackQuery)
{
_logger.LogInformation("todo {user}", dbUser);
await _botClient.AutoReplyAsync("未实现", query, false);
await _botClient.AutoReplyAsync("未实现", callbackQuery, false);
}

}
17 changes: 10 additions & 7 deletions XinjingdailyBot.Command/CommonCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@ public CommonCommand(
_commandHandler = commandHandler;
}

/// <inheritdoc cref="IBanRecordService.WarningLimit"/>
private static int WarningLimit = IBanRecordService.WarningLimit;

/// <inheritdoc cref="IBanRecordService.WarnDuration"/>
private static int WarnDuration = IBanRecordService.WarnDuration;
private readonly int WarnDuration = IBanRecordService.WarnDuration;

/// <summary>
/// 显示命令帮助
Expand Down Expand Up @@ -152,8 +149,6 @@ public async Task ResponseMyBan(Users dbUser, Message message)
.Where(x => x.UserID == dbUser.UserID && (x.Type != EBanType.Warning || x.BanTime > expireTime))
.ToListAsync();

records = records.OrderByDescending(static x => x.BanTime.GetTimestamp()).ToList();

var sb = new StringBuilder();

string status = dbUser.IsBan ? "已封禁" : "正常";
Expand All @@ -179,9 +174,17 @@ public async Task ResponseMyBan(Users dbUser, Message message)
EBanType.UnBan => "解封",
EBanType.Ban => "封禁",
EBanType.Warning => "警告",
EBanType.GlobalMute => "全局禁言",
EBanType.GlobalBan => "全局封禁",
EBanType.GlobalUnMute => "撤销全局禁言",
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();
}
}
}
sb.AppendLine("\n仅显示90天内的警告记录");
Expand Down
2 changes: 1 addition & 1 deletion XinjingdailyBot.Infrastructure/Enums/EPostStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ public enum EPostStatus : int
/// <summary>
/// 已过审并发布在第二频道
/// </summary>
AcceptSecond,
AcceptedSecond,
}
4 changes: 2 additions & 2 deletions XinjingdailyBot.Service/Data/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public async Task AcceptPost(NewPosts post, Users dbUser, bool inPlan, bool seco

bool hasSpoiler = post.HasSpoiler;

Message? publicMsg=null;
Message? publicMsg = null;

if (!inPlan)
{
Expand Down Expand Up @@ -785,7 +785,7 @@ public async Task AcceptPost(NewPosts post, Users dbUser, bool inPlan, bool seco
}

await _botClient.AutoReplyAsync("稿件已发布", callbackQuery);
post.Status = EPostStatus.Accepted;
post.Status = !second ? EPostStatus.Accepted : EPostStatus.AcceptedSecond;
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions XinjingdailyBot.WebAPI/Extensions/WebAPIExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public static void AddWebAPI(this IServiceCollection services, IWebHostBuilder w
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//获取客户端 IP
services.Configure<ForwardedHeadersOptions>(options =>
{
services.Configure<ForwardedHeadersOptions>(options => {
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
Expand Down
Loading

0 comments on commit fdc8741

Please sign in to comment.