Archived
1
0
Fork 0

Add group blocklist

This commit is contained in:
多羅狼 2024-03-22 16:17:36 +08:00 committed by GitHub
parent da75f29b77
commit 55b42dd56b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 82 additions and 18 deletions

View file

@ -169,6 +169,7 @@ async def _(event: Event):
await tos_report('QQ|' + str(event.operator_id), 'QQ|Group|' + str(event.group_id), reason, banned=True)
await bot.call_action('set_group_leave', group_id=event.group_id)
BotDBUtil.SenderInfo('QQ|' + str(event.operator_id)).edit('isInBlockList', True)
BotDBUtil.GroupBlockList.add('QQ|Group|' + str(event.group_id))
await bot.call_action('delete_friend', friend_id=event.operator_id)
@ -180,18 +181,20 @@ async def _(event: Event):
reason = Locale(lang).t('tos.message.reason.kick')
await tos_report('QQ|' + str(event.operator_id), 'QQ|Group|' + str(event.group_id), reason, banned=True)
BotDBUtil.SenderInfo('QQ|' + str(event.operator_id)).edit('isInBlockList', True)
BotDBUtil.GroupBlockList.add('QQ|Group|' + str(event.group_id))
await bot.call_action('delete_friend', friend_id=event.operator_id)
"""
@bot.on_message('group')
async def _(event: Event):
if Config('qq_group_allow_list'):
result = BotDBUtil.isGroupInAllowList(f'QQ|Group|{str(event.group_id)}')
if not result:
await bot.send(event=event, message='此群不在白名单中,已自动退群。'
'\n如需申请白名单请至https://github.com/Teahouse-Studios/bot/issues/new/choose发起issue。')
result = BotDBUtil.GroupBlockList.check(f'QQ|Group|{str(event.group_id)}')
if result:
res = Locale(lang).t('qq.message.in_group_blocklist')
if Config('issue_url'):
res += '\n' + Locale(lang).t('tos.message.appeal', issue_url=Config('issue_url'))
await bot.send(event=event, message=str(res))
await bot.call_action('set_group_leave', group_id=event.group_id)
"""
qq_host = Config("qq_host")
if qq_host:

View file

@ -178,6 +178,7 @@ async def _(event: Event):
await tos_report('QQ|' + str(event.operator_id), 'QQ|Group|' + str(event.group_id), reason, banned=True)
await bot.call_action('set_group_leave', group_id=event.group_id)
BotDBUtil.SenderInfo('QQ|' + str(event.operator_id)).edit('isInBlockList', True)
BotDBUtil.GroupBlockList.add('QQ|Group|' + str(event.group_id))
await bot.call_action('delete_friend', friend_id=event.operator_id)
@ -189,6 +190,7 @@ async def _(event: Event):
reason = Locale(lang).t('tos.message.reason.kick')
await tos_report('QQ|' + str(event.operator_id), 'QQ|Group|' + str(event.group_id), reason, banned=True)
BotDBUtil.SenderInfo('QQ|' + str(event.operator_id)).edit('isInBlockList', True)
BotDBUtil.GroupBlockList.add('QQ|Group|' + str(event.group_id))
await bot.call_action('delete_friend', friend_id=event.operator_id)

View file

@ -279,16 +279,32 @@ class BotDBUtil:
session.commit()
session.expire_all()
class GroupBlockList:
@staticmethod
@retry(stop=stop_after_attempt(3))
@auto_rollback_error
def isGroupInAllowList(target_id):
def check(target_id):
session.expire_all()
query = session.query(GroupAllowList).filter_by(targetId=target_id).first()
query = session.query(GroupBlockList).filter_by(targetId=target_id).first()
if query:
return True
return False
def add(target_id):
session.add(GroupBlockList(targetId=target_id))
session.commit()
return True
def remove(target_id):
entry = session.query(GroupBlockList).filter_by(targetId=target_id).first()
if entry:
session.delete(entry)
session.commit()
return True
else:
return False
class Data:
def __init__(self, msg: Union[MessageSession, FetchTarget, str]):
if isinstance(msg, MessageSession):

View file

@ -45,8 +45,8 @@ class CommandTriggerTime(Base):
timestamp = Column(TIMESTAMP, default=text('CURRENT_TIMESTAMP'))
class GroupAllowList(Base):
__tablename__ = "GroupAllowList"
class GroupBlockList(Base):
__tablename__ = "GroupBlockList"
targetId = Column(String(512), primary_key=True)
@ -93,5 +93,5 @@ class JobQueueTable(Base):
Session.create()
__all__ = ["SenderInfo", "TargetInfo", "CommandTriggerTime", "GroupAllowList",
__all__ = ["SenderInfo", "TargetInfo", "CommandTriggerTime", "GroupBlockList",
"StoredData", "DBVersion", "AnalyticsData", "UnfriendlyActionsTable", "JobQueueTable"]

View file

@ -66,6 +66,7 @@
"qq.message.disable_friend_request": "你好!本机器人不主动同意好友请求,请联系开发者添加好友。",
"qq.message.disable_group_invite": "你好!本机器人不主动同意入群请求,请联系开发者邀请加入群组。",
"qq.message.disable_temp_session": "请先添加好友后再进行命令交互。",
"qq.message.in_group_blocklist": "此群组在黑名单中,已自动退出。",
"success": "成功。",
"time.date.format": "%Y 年 %m 月 %d 日",
"time.date.iso.format": "%Y-%m-%d",

View file

@ -35,12 +35,15 @@
"core.help.version": "View bot version.",
"core.help.whoami": "Get the ID of the user account that sent the command inside the bot.",
"core.message.abuse.ban.success": "Successfully banned ${user}.",
"core.message.abuse.block.self": "You cannot do this in this conversation!",
"core.message.abuse.block.success": "Successfully add ${target} into blocklist.",
"core.message.abuse.check.banned": "(This user has been banned)",
"core.message.abuse.check.tempbanned": "(This user has been temporarily banned. ${ban_time} second(s) remaining until unban)",
"core.message.abuse.check.warns": "${user} has been warned ${warns} time(s).",
"core.message.abuse.clear.success": "Successfully cleared warning for ${user}.",
"core.message.abuse.revoke.success": "Successfully removed ${count} warning(s) for ${user}. This user has been warned ${warn_count} time(s).",
"core.message.abuse.unban.success": "Successfully unbanned ${user}.",
"core.message.abuse.unblock.success": "Successfully remove ${target} from blocklist.",
"core.message.abuse.untempban.success": "Successfully removed temporary ban on ${user}.",
"core.message.abuse.warn.success": "Successfully warned ${user} ${count} time(s). This user has been warned ${warn_count} time(s).",
"core.message.admin.add.success": "Success: ${user} was set as bot administrator.",

View file

@ -35,12 +35,15 @@
"core.help.version": "查看机器人的版本号。",
"core.help.whoami": "获取发送命令的账号在机器人内部的 ID。",
"core.message.abuse.ban.success": "成功封禁 ${user}。",
"core.message.abuse.block.self": "你不可以在本对话内进行此操作!",
"core.message.abuse.block.success": "成功将 ${target} 加入黑名单。",
"core.message.abuse.check.banned": "(此用户已被封禁)",
"core.message.abuse.check.tempbanned": "(此用户已被临时封禁,距离解封时间还有 ${ban_time} 秒)",
"core.message.abuse.check.warns": "${user} 已被警告 ${warns} 次。",
"core.message.abuse.clear.success": "成功清除 ${user} 的警告。",
"core.message.abuse.revoke.success": "成功移除警告 ${user} 的 ${count} 次警告。此用户已被警告 ${warn_count} 次。",
"core.message.abuse.unban.success": "成功解除 ${user} 的封禁。",
"core.message.abuse.unblock.success": "成功将 ${target} 移除黑名单。",
"core.message.abuse.untempban.success": "成功解除 ${user} 的临时封禁。",
"core.message.abuse.warn.success": "成功警告 ${user} ${count} 次。此用户已被警告 ${warn_count} 次。",
"core.message.admin.add.success": "成功:已将 ${user} 设置为机器人管理员。",

View file

@ -15,6 +15,9 @@
"core.help.leave": "使機器人離開群組。",
"core.help.locale.desc": "控制機器人語言。",
"core.help.locale.set": "設定機器人使用的語言。",
"core.message.abuse.block.self": "你不可以在本對話內進行此操作!",
"core.message.abuse.block.success": "成功將 ${target} 加入黑名單。",
"core.message.abuse.unblock.success": "成功將 ${target} 移除黑名單。",
"core.help.module.disable": "停用一個/多個模組。",
"core.help.module.disable_all": "停用所有模組。",
"core.help.module.enable": "啟用一個/多個模組。",

View file

@ -197,6 +197,39 @@ async def _(msg: Bot.MessageSession, user: str):
await msg.finish(msg.locale.t("core.message.abuse.unban.success", user=user))
@ae.command('block <target>')
async def _(msg: Bot.MessageSession, target: str):
if not target.startswith(f'{msg.target.target_from}|'):
await msg.finish(msg.locale.t("message.id.invalid.target", target=msg.target.target_from))
if target == msg.target.target_id:
await msg.finish(msg.locale.t("core.message.abuse.block.self"))
if BotDBUtil.GroupBlockList.add(target):
await msg.finish(msg.locale.t("core.message.abuse.block.success", target=target))
@ae.command('unblock <target>')
async def _(msg: Bot.MessageSession, target: str):
if not target.startswith(f'{msg.target.target_from}|'):
await msg.finish(msg.locale.t("message.id.invalid.target", target=msg.target.target_from))
if BotDBUtil.GroupBlockList.remove(target):
await msg.finish(msg.locale.t("core.message.abuse.unblock.success", target=target))
res = module('reset', required_superuser=True, base=True)
@res.command()
async def reset(msg: Bot.MessageSession):
confirm = await msg.wait_confirm(msg.locale.t("core.message.confirm"), append_instruction=False)
if confirm:
pull_repo_result = os.popen('git reset --hard origin/master', 'r').read()[:-1]
if pull_repo_result != '':
await msg.finish(pull_repo_result)
else:
await msg.finish(msg.locale.t("core.message.update.failed"))
else:
await msg.finish()
upd = module('update', required_superuser=True, base=True)