Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Jun 2, 2023
1 parent 843f694 commit af55d81
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 33 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.1.0</Version>
<Version>2.1.1.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
4 changes: 3 additions & 1 deletion XinjingdailyBot.Command/AdminCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
Expand Down Expand Up @@ -1013,8 +1014,9 @@ public async Task SetUserGroup(Users dbUser, Message message, string[] args)
}

var keyboard = await _markupHelperService.SetUserGroupKeyboard(dbUser, targetUser);
var groupName = _groupRepository.GetGroupById(targetUser.GroupID)?.Name ?? "未知";

return (keyboard != null ? "请选择新的用户组" : "获取可用用户组失败", keyboard);
return (keyboard != null ? $"请选择 {targetUser.EscapedFullName()} 的新用户组, 当前用户组 {groupName}" : "获取可用用户组失败", keyboard);
}

(var text, var kbd) = await exec();
Expand Down
12 changes: 12 additions & 0 deletions XinjingdailyBot.Interface/Bot/Common/IChannelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,17 @@ public interface IChannelService
/// <param name="chat"></param>
/// <returns></returns>
bool IsGroupMessage(Chat chat);
/// <summary>
/// 判断是否是审核群组的信息
/// </summary>
/// <param name="chatId"></param>
/// <returns></returns>
bool IsReviewMessage(long chatId);
/// <summary>
/// 判断是否是审核群组的信息
/// </summary>
/// <param name="chat"></param>
/// <returns></returns>
bool IsReviewMessage(Chat chat);
}
}
9 changes: 9 additions & 0 deletions XinjingdailyBot.Service/Bot/Common/ChannelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,14 @@ public bool IsGroupMessage(Chat chat)
{
return chat.Id == _subGroup.Id || chat.Id == _commentGroup.Id;
}

public bool IsReviewMessage(long chatId)
{
return chatId == _reviewGroup.Id;
}
public bool IsReviewMessage(Chat chat)
{
return chat.Id == _reviewGroup.Id;
}
}
}
31 changes: 22 additions & 9 deletions XinjingdailyBot.Service/Bot/Handler/GroupMessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Telegram.Bot;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using XinjingdailyBot.Infrastructure.Attribute;
Expand All @@ -16,13 +17,16 @@ internal class GroupMessageHandler : IGroupMessageHandler
{
private readonly IChannelService _channelService;
private readonly ITelegramBotClient _botClient;
private readonly ILogger<GroupMessageHandler> _logger;

public GroupMessageHandler(
ITelegramBotClient botClient,
IChannelService channelService)
IChannelService channelService,
ILogger<GroupMessageHandler> logger)
{
_botClient = botClient;
_channelService = channelService;
_logger = logger;
}

public async Task OnGroupTextMessageReceived(Users dbUser, Message message)
Expand All @@ -43,20 +47,29 @@ public async Task OnGroupTextMessageReceived(Users dbUser, Message message)
}
else
{
int seconds = rand.Next(60, 300);
DateTime banTime = DateTime.Now + TimeSpan.FromSeconds(seconds);
int seconds = rand.Next(45, 315);
DateTime banTime = DateTime.Now.AddSeconds(seconds);

var chatId = message.Chat.Id;

var sendMsg = await _botClient.AutoReplyAsync($"学我说话很好玩{Emojis.Horse}? 劳资反手就是禁言 <code>{seconds}</code> 秒.", message, ParseMode.Html);
try
{
ChatPermissions permission = new() { CanSendMessages = false, };
var permission = new ChatPermissions {
CanSendMessages = false,
CanSendAudios = false,
CanSendDocuments = false,
CanSendPhotos = false,
CanSendVideos = false,
CanSendVideoNotes = false,
CanSendVoiceNotes = false,
CanSendPolls = false,
CanSendOtherMessages = false,
};
await _botClient.RestrictChatMemberAsync(chatId, dbUser.UserID, permission, untilDate: banTime);
var sendMsg = await _botClient.AutoReplyAsync($"学我说话很好玩{Emojis.Horse}? 劳资反手就是禁言 <code>{seconds}</code> 秒.", message, ParseMode.Html);
}
catch
catch (Exception ex)
{
await _botClient.DeleteMessageAsync(chatId, sendMsg.MessageId);
_logger.LogError(ex, "禁言失败");
await _botClient.AutoReplyAsync("原来是狗管理, 惹不起惹不起...", message);
}
}
Expand Down
6 changes: 5 additions & 1 deletion XinjingdailyBot.Service/Bot/Handler/MessageHandler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using Telegram.Bot;
using Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using XinjingdailyBot.Infrastructure;
using XinjingdailyBot.Infrastructure.Attribute;
using XinjingdailyBot.Infrastructure.Extensions;
using XinjingdailyBot.Interface.Bot.Common;
using XinjingdailyBot.Interface.Bot.Handler;
using XinjingdailyBot.Interface.Data;
using XinjingdailyBot.Model.Models;
using static XinjingdailyBot.Infrastructure.OptionsSetting;

namespace XinjingdailyBot.Service.Bot.Handler
{
Expand Down
7 changes: 2 additions & 5 deletions XinjingdailyBot.Service/Data/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,13 @@ private async Task AutoLeaveChat(Chat? msgChat)
{
case ChatType.Group:
case ChatType.Supergroup:
if (msgChat.Id != _channelService.SubGroup.Id &&
msgChat.Id != _channelService.CommentGroup.Id &&
msgChat.Id != _channelService.ReviewGroup.Id)
if (!_channelService.IsGroupMessage(msgChat) && !_channelService.IsReviewMessage(msgChat))
{
autoLeave = true;
}
break;
case ChatType.Channel:
if (msgChat.Id != _channelService.AcceptChannel.Id &&
msgChat.Id != _channelService.RejectChannel.Id)
if (!_channelService.IsChannelMessage(msgChat))
{
autoLeave = true;
}
Expand Down
22 changes: 6 additions & 16 deletions XinjingdailyBot.Service/Helper/MarkupHelperService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public InlineKeyboardMarkup ReviewKeyboardA(int tagNum, bool? hasSpoiler)
foreach (var tag in tags)
{
line.Add(InlineKeyboardButton.WithCallbackData(tag.DisplayName, $"review tag {tag.Payload}"));
lineChars += tag.DisplayName.Length -1;
lineChars += tag.DisplayName.Length - 1;
if (lineChars >= IMarkupHelperService.MaxLineChars)
{
lineChars = 0;
Expand Down Expand Up @@ -183,35 +183,25 @@ public InlineKeyboardMarkup ReviewKeyboardB()
}
else
{
int lineCount = groups.Count <= 4 ? 2 : 3;

var btns = new List<IEnumerable<InlineKeyboardButton>>();
var line = new List<InlineKeyboardButton>();

foreach (var group in groups)
{
var name = targetUser.GroupID == group.Id ? $"当前用户组: [ {group.Id}. {group.Name} ]" : $"{group.Id}. {group.Name}";
var name = targetUser.GroupID == group.Id ? $"[ {group.Id}. {group.Name} ]" : $"{group.Id}. {group.Name}";
var data = $"cmd {dbUser.UserID} setusergroup {targetUser.UserID} {group.Id}";

line.Add(InlineKeyboardButton.WithCallbackData(name, data));
if (line.Count >= lineCount)
btns.Add(new[]
{
btns.Add(line);
line = new();
}
}

if (line.Any())
{
btns.Add(line);
InlineKeyboardButton.WithCallbackData(name, data),
});
}

btns.Add(new[]
{
InlineKeyboardButton.WithCallbackData("取消操作", $"cmd {dbUser.UserID} cancel"),
});

InlineKeyboardMarkup keyboard = new(btns);
var keyboard = new InlineKeyboardMarkup(btns);

return keyboard;
}
Expand Down

0 comments on commit af55d81

Please sign in to comment.