Skip to content

Commit

Permalink
Update tos
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroWolf authored Mar 20, 2024
1 parent e093610 commit 38b1570
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
23 changes: 15 additions & 8 deletions core/parser/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from datetime import datetime

from config import Config
from core.builtins import command_prefix, ExecutionLockList, ErrorMessage, MessageTaskManager, Url, Bot, \
base_superuser_list
from core.builtins import Bot, ErrorMessage, ExecutionLockList, I18NContext, MessageTaskManager, Url, \
base_superuser_list, command_prefix
from core.exceptions import AbuseWarning, FinishedException, InvalidCommandFormatError, InvalidHelpDocTypeError, \
WaitCancelException, NoReportException, SendMessageFailed
from core.loader import ModulesManager, current_unloaded_modules, err_modules
Expand Down Expand Up @@ -43,13 +43,20 @@ async def remove_temp_ban(target):
del temp_ban_counter[target]


async def tos_abuse_warning(msg: Bot.MessageSession, e):
async def tos_abuse_warning(msg: Bot.MessageSession, e, i18n=False):
if isinstance(e, I18NContext):
reason = e.to_dict()['data']['key']
i18n = True
else:
reason = str(e)
if enable_tos and Config('tos_warning_counts', 5) >= 1 and not msg.check_super_user():
await warn_target(msg, str(e))
await warn_target(msg, reason, i18n)
temp_ban_counter[msg.target.sender_id] = {'count': 1,
'ts': datetime.now().timestamp()}
else:
await msg.send_message(msg.locale.t("error.prompt.noreport", detail=e))
if i18n:
reason = msg.locale.t(reason)
await msg.send_message(msg.locale.t("error.prompt.noreport", detail=reason))


async def tos_msg_counter(msg: Bot.MessageSession, command: str):
Expand All @@ -61,15 +68,15 @@ async def tos_msg_counter(msg: Bot.MessageSession, command: str):
else:
same['count'] += 1
if same['count'] > 10:
raise AbuseWarning(msg.locale.t("tos.message.reason.cooldown"))
raise AbuseWarning(I18NContext("tos.message.reason.cooldown"))
all_ = counter_all.get(msg.target.sender_id)
if not all_ or datetime.now().timestamp() - all_['ts'] > 300: # 检查是否滥用(5分钟内使用20条命令)
counter_all[msg.target.sender_id] = {'count': 1,
'ts': datetime.now().timestamp()}
else:
all_['count'] += 1
if all_['count'] > 20:
raise AbuseWarning(msg.locale.t("tos.message.reason.abuse"))
raise AbuseWarning(I18NContext("tos.message.reason.abuse"))


async def temp_ban_check(msg: Bot.MessageSession):
Expand All @@ -87,7 +94,7 @@ async def temp_ban_check(msg: Bot.MessageSession):
is_temp_banned['count'] += 1
await msg.finish(msg.locale.t("tos.message.tempbanned.warning", ban_time=int(TOS_TEMPBAN_TIME - ban_time)))
else:
raise AbuseWarning(msg.locale.t("tos.message.reason.ignore"))
raise AbuseWarning(I18NContext("tos.message.reason.ignore"))


async def parser(msg: Bot.MessageSession, require_enable_modules: bool = True, prefix: list = None,
Expand Down
22 changes: 14 additions & 8 deletions core/tos.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
WARNING_COUNTS = Config('tos_warning_counts', 5)


async def warn_target(msg: Bot.MessageSession, reason = None):
async def warn_target(msg: Bot.MessageSession, reason=None, i18n=False):
if WARNING_COUNTS >= 1 and not msg.check_super_user():
current_warns = int(msg.target.sender_info.query.warns) + 1
msg.target.sender_info.edit('warns', current_warns)
warn_template = [msg.locale.t("tos.message.warning")]
if reason:
warn_template.append(msg.locale.t("tos.message.reason") + reason)
if i18n:
warn_template.append(msg.locale.t("tos.message.reason") + msg.locale.t(reason))
else:
warn_template.append(msg.locale.t("tos.message.reason") + reason)
if current_warns < WARNING_COUNTS:
await tos_report(msg.target.sender_id, msg.target.target_id, reason)
await tos_report(msg.target.sender_id, msg.target.target_id, reason, i18n=i18n)
warn_template.append(
msg.locale.t(
'tos.message.warning.count',
Expand All @@ -25,11 +28,11 @@ async def warn_target(msg: Bot.MessageSession, reason = None):
if current_warns <= 2 and Config('issue_url'):
warn_template.append(msg.locale.t('tos.message.appeal', issue_url=Config('issue_url')))
elif current_warns == WARNING_COUNTS:
await tos_report(msg.target.sender_id, msg.target.target_id, reason)
await tos_report(msg.target.sender_id, msg.target.target_id, reason, i18n=i18n)
warn_template.append(msg.locale.t('tos.message.warning.last'))
elif current_warns > WARNING_COUNTS:
msg.target.sender_info.edit('isInBlockList', True)
await tos_report(msg.target.sender_id, msg.target.target_id, reason, banned=True)
await tos_report(msg.target.sender_id, msg.target.target_id, reason, banned=True, i18n=i18n)
warn_template.append(msg.locale.t('tos.message.banned'))
if Config('issue_url'):
warn_template.append(msg.locale.t('tos.message.appeal', issue_url=Config('issue_url')))
Expand All @@ -40,20 +43,23 @@ async def pardon_user(user: str):
BotDBUtil.SenderInfo(user).edit('warns', 0)


async def warn_user(user: str, count = 1):
async def warn_user(user: str, count=1):
current_warns = int(BotDBUtil.SenderInfo(user).query.warns) + count
BotDBUtil.SenderInfo(user).edit('warns', current_warns)
if current_warns > WARNING_COUNTS and WARNING_COUNTS >= 1:
BotDBUtil.SenderInfo(user).edit('isInBlockList', True)
return current_warns


async def tos_report(sender, target, reason = None, banned = False):
async def tos_report(sender, target, reason=None, banned=False, i18n=False):
locale = Locale(lang)
if report_targets:
warn_template = [locale.t("tos.message.report", sender=sender, target=target)]
if reason:
warn_template.append(locale.t("tos.message.reason") + reason)
if i18n:
warn_template.append(locale.t("tos.message.reason") + locale.t(reason))
else:
warn_template.append(locale.t("tos.message.reason") + reason)
if banned:
action = locale.t("tos.message.action.banned")
else:
Expand Down
6 changes: 3 additions & 3 deletions modules/wiki/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import filetype

from core.builtins import Bot, Plain, Image, Voice, Url, confirm_command
from core.builtins import Bot, Plain, I18NContext, Image, Voice, Url, confirm_command
from core.types import MessageSession
from core.utils.image_table import image_table_render, ImageTable
from core.component import module
Expand Down Expand Up @@ -86,7 +86,7 @@ async def query_pages(session: Union[Bot.MessageSession, QueryInfo], title: Unio
if isinstance(title, str):
title = [title]
if len(title) > 15:
raise AbuseWarning(session.locale.t('tos.message.reason.wiki_abuse'))
raise AbuseWarning(I18NContext('tos.message.reason.wiki_abuse'))
query_task = {start_wiki: {'query': [], 'iw_prefix': ''}}
for t in title:
if prefix and use_prefix:
Expand Down Expand Up @@ -315,7 +315,7 @@ async def _callback(msg: Bot.MessageSession):
wait_msg_list.append(
Plain('\n'.join(wait_plain_slice)))
except WhatAreUDoingError:
raise AbuseWarning(session.locale.t('tos.message.reason.too_many_redirects'))
raise AbuseWarning(I18NContext('tos.message.reason.too_many_redirects'))
except InvalidWikiError as e:
if isinstance(session, Bot.MessageSession):
await session.send_message(session.locale.t('error') + str(e))
Expand Down

0 comments on commit 38b1570

Please sign in to comment.