one key ban in bot

This commit is contained in:
BennyThink
2022-03-26 11:17:56 +08:00
parent 24e7483c62
commit 832c9f94bf
3 changed files with 69 additions and 0 deletions

View File

@@ -182,6 +182,32 @@ def send_search(message):
base_send_search(message) base_send_search(message)
@bot.message_handler(content_types=["document"])
def ban_user(message):
if str(message.chat.id) != MAINTAINER:
return
mem = io.BytesIO()
file_id = message.document.file_id
file_info = bot.get_file(file_id)
content = bot.download_file(file_info.file_path)
mem.write(content)
user_list = mem.getvalue().decode("u8").split("\n")
yy = fansub.YYeTsOffline()
client = yy.mongo
user_col = client["zimuzu"]["users"]
text = ""
for line in user_list:
user, reason = line.split(maxsplit=1)
ban = {"disable": True, "reason": reason}
user_col.update_one({"username": user}, {"$set": {"status": ban}})
status = f"{user} 已经被禁言,原因:{reason}\n"
logging.info("Banning %s", status)
text += status
bot.reply_to(message, text)
mem.close()
def base_send_search(message, instance=None): def base_send_search(message, instance=None):
if instance is None: if instance is None:
fan = fansub.FansubEntrance() fan = fansub.FansubEntrance()

View File

@@ -0,0 +1,22 @@
#!/usr/local/bin/python3
# coding: utf-8
# YYeTsBot - ban_user.py
# 3/26/22 10:26
#
__author__ = "Benny <benny.think@gmail.com>"
from common import Mongo
from tqdm import tqdm
client = Mongo()
user_col = client.db["users"]
with open("ban_user.txt", "r") as f:
for line in tqdm(f, desc="Banning user..."):
user, reason = line.split(maxsplit=1)
ban = {"disable": True, "reason": reason}
user_col.update_one({"username": user}, {"$set": {"status": ban}})
print("Done!")

View File

@@ -0,0 +1,21 @@
#!/usr/local/bin/python3
# coding: utf-8
# YYeTsBot - common.py
# 3/26/22 10:40
#
__author__ = "Benny <benny.think@gmail.com>"
import pymongo
import os
class Mongo:
def __init__(self):
self.client = pymongo.MongoClient(host=os.getenv("mongo") or "localhost", connect=False,
connectTimeoutMS=5000, serverSelectionTimeoutMS=5000)
self.db = self.client["zimuzu"]
def __del__(self):
self.client.close()