-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.py
93 lines (79 loc) · 3.53 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from telegram import Update
from telegram.ext import ContextTypes, ConversationHandler
from palworld import get_players, send_announcement, kick_player, ban_player, unban_player
# 定义一个全局变量来存储管理员密码
ADMIN_PASSWORD = getenv("ADMIN_PASSWORD")
PASSWORD, = range(1)
# 定义启动命令的处理函数
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Welcome to GijinkaBot!")
# 定义身份验证命令的处理函数
async def auth_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("请在私聊框中输入管理员密码")
return PASSWORD
# 处理用户输入的密码
async def handle_auth(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message.text == ADMIN_PASSWORD:
context.user_data["is_authenticated"] = True
await update.message.reply_text("身份验证成功!")
# 删除用户输入的密码消息
await update.message.delete()
return ConversationHandler.END
else:
await update.message.reply_text("密码错误,请重新输入:")
# 删除用户输入的错误密码消息
await update.message.delete()
return PASSWORD
# 定义一个装饰器函数来检查身份验证状态
def require_auth(func):
async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if not context.user_data.get("is_authenticated"):
await update.message.reply_text("请先使用/auth命令进行身份验证。")
return
return await func(update, context)
return wrapper
# 定义获取服务器信息的处理函数
async def players_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
message = await get_players()
await update.message.reply_text(message)
# 定义全服播报命令的处理函数
@require_auth
async def announce_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
message_text = " ".join(context.args)
if not message_text:
await update.message.reply_text("请输入要播报的消息。")
return
response = await send_announcement(message_text)
await update.message.reply_text(response)
# 定义踢出玩家的处理函数
@require_auth
async def kick_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
args = context.args
if len(args) < 2:
await update.message.reply_text("请输入正确的命令格式:/kick <玩家ID> <踢出原因>")
return
user_id = args[0]
reason = " ".join(args[1:])
response = await kick_player(user_id, reason)
await update.message.reply_text(response)
# 定义封禁玩家的处理函数
@require_auth
async def ban_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
args = context.args
if len(args) < 2:
await update.message.reply_text("请输入正确的命令格式:/ban <玩家ID> <封禁原因>")
return
user_id = args[0]
reason = " ".join(args[1:])
response = await ban_player(user_id, reason)
await update.message.reply_text(response)
# 定义解禁玩家的处理函数
@require_auth
async def unban_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
args = context.args
if len(args) < 1:
await update.message.reply_text("请输入正确的命令格式:/unban <玩家ID>")
return
user_id = args[0]
response = await unban_player(user_id)
await update.message.reply_text(response)