mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-06-20 19:38:00 +00:00
Add sudo.block
This commit is contained in:
parent
ca2ecbda94
commit
015c669485
@ -1,10 +1,103 @@
|
|||||||
from command import CommandRegistry
|
import sqlite3
|
||||||
|
|
||||||
|
from command import CommandRegistry, split_args
|
||||||
from commands import core
|
from commands import core
|
||||||
|
from little_shit import get_default_db_path, get_target
|
||||||
|
|
||||||
__registry__ = cr = CommandRegistry()
|
__registry__ = cr = CommandRegistry()
|
||||||
|
|
||||||
|
_create_table_sql = """CREATE TABLE IF NOT EXISTS blocked_target_list (
|
||||||
|
target TEXT NOT NULL
|
||||||
|
)"""
|
||||||
|
|
||||||
|
|
||||||
|
def _open_db_conn():
|
||||||
|
conn = sqlite3.connect(get_default_db_path())
|
||||||
|
conn.execute(_create_table_sql)
|
||||||
|
conn.commit()
|
||||||
|
return conn
|
||||||
|
|
||||||
|
|
||||||
@cr.register('test')
|
@cr.register('test')
|
||||||
@cr.restrict(full_command_only=True, superuser_only=True)
|
@cr.restrict(full_command_only=True, superuser_only=True)
|
||||||
def test(_, ctx_msg):
|
def test(_, ctx_msg):
|
||||||
core.echo('Your are the superuser!', ctx_msg)
|
core.echo('Your are the superuser!', ctx_msg)
|
||||||
|
|
||||||
|
|
||||||
|
@cr.register('block')
|
||||||
|
@cr.restrict(full_command_only=True, superuser_only=True)
|
||||||
|
@split_args(maxsplit=2)
|
||||||
|
def block(args, ctx_msg):
|
||||||
|
def _send_error_msg():
|
||||||
|
core.echo('参数不正确。\n\n正确使用方法:\nsudo.block wx|qq <account-to-block>', ctx_msg)
|
||||||
|
|
||||||
|
if len(args) != 2:
|
||||||
|
_send_error_msg()
|
||||||
|
return
|
||||||
|
|
||||||
|
via, account = args
|
||||||
|
# Get a target using a fake context message
|
||||||
|
target = get_target({
|
||||||
|
'via': via,
|
||||||
|
'type': 'friend_message',
|
||||||
|
'sender_uid': account,
|
||||||
|
'sender_account': account
|
||||||
|
})
|
||||||
|
|
||||||
|
if not target:
|
||||||
|
_send_error_msg()
|
||||||
|
return
|
||||||
|
|
||||||
|
conn = _open_db_conn()
|
||||||
|
conn.execute('INSERT INTO blocked_target_list (target) VALUES (?)', (target,))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
core.echo('成功屏蔽用户 ' + account, ctx_msg)
|
||||||
|
|
||||||
|
|
||||||
|
@cr.register('block_list', 'block-list')
|
||||||
|
@cr.restrict(full_command_only=True, superuser_only=True)
|
||||||
|
def block_list(_, ctx_msg, internal=False):
|
||||||
|
conn = _open_db_conn()
|
||||||
|
cursor = conn.execute('SELECT target FROM blocked_target_list')
|
||||||
|
blocked_targets = list(set([x[0] for x in list(cursor)])) # Get targets and remove duplications
|
||||||
|
conn.close()
|
||||||
|
if internal:
|
||||||
|
return blocked_targets
|
||||||
|
if blocked_targets:
|
||||||
|
# `t[1:]` to reply user account, without target prefix 'p'.
|
||||||
|
# This is a shit code, and should be changed later sometime.
|
||||||
|
core.echo('已屏蔽的用户:\n' + ', '.join([t[1:] for t in blocked_targets]), ctx_msg)
|
||||||
|
else:
|
||||||
|
core.echo('还没有屏蔽过用户')
|
||||||
|
|
||||||
|
|
||||||
|
@cr.register('unblock')
|
||||||
|
@cr.restrict(full_command_only=True, superuser_only=True)
|
||||||
|
@split_args(maxsplit=2)
|
||||||
|
def unblock(args, ctx_msg):
|
||||||
|
def _send_error_msg():
|
||||||
|
core.echo('参数不正确。\n\n正确使用方法:\nsudo.unblock wx|qq <account-to-unblock>', ctx_msg)
|
||||||
|
|
||||||
|
if len(args) != 2:
|
||||||
|
_send_error_msg()
|
||||||
|
return
|
||||||
|
|
||||||
|
via, account = args
|
||||||
|
# Get a target using a fake context message
|
||||||
|
target = get_target({
|
||||||
|
'via': via,
|
||||||
|
'type': 'friend_message',
|
||||||
|
'sender_uid': account,
|
||||||
|
'sender_account': account
|
||||||
|
})
|
||||||
|
|
||||||
|
if not target:
|
||||||
|
_send_error_msg()
|
||||||
|
return
|
||||||
|
|
||||||
|
conn = _open_db_conn()
|
||||||
|
conn.execute('DELETE FROM blocked_target_list WHERE target = ?', (target,))
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
core.echo('成功取消屏蔽用户 ' + account, ctx_msg)
|
||||||
|
18
filters/intercept_blocked_target_100.py
Normal file
18
filters/intercept_blocked_target_100.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
"""
|
||||||
|
This filter intercepts messages from blocked targets (blocked using sudo.block command).
|
||||||
|
"""
|
||||||
|
|
||||||
|
from commands import sudo
|
||||||
|
from filter import as_filter
|
||||||
|
from little_shit import get_target
|
||||||
|
|
||||||
|
|
||||||
|
@as_filter(priority=100)
|
||||||
|
def _filter(ctx_msg):
|
||||||
|
target = get_target(ctx_msg)
|
||||||
|
if not target:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if target in sudo.block_list('', ctx_msg, internal=True):
|
||||||
|
return False
|
||||||
|
return True
|
Loading…
x
Reference in New Issue
Block a user