-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtg.py
More file actions
104 lines (81 loc) · 3.68 KB
/
Copy pathtg.py
File metadata and controls
104 lines (81 loc) · 3.68 KB
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
94
95
96
97
98
99
100
101
102
103
104
import logging
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, LinkPreviewOptions, BotCommand
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, CallbackQueryHandler, MessageHandler, filters, ConversationHandler
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
import dotenv
import os
dotenv.load_dotenv()
token = os.getenv('TG_BOT_TOKEN')
application = ApplicationBuilder().token(token).build() # 初始化应用
commands = [] # 命令列表
# 装饰器,自动注册命令
def command(description, name=None):
def decorator(func):
commands.append(BotCommand(name or func.__name__, description))
application.add_handler(CommandHandler(func.__name__, func))
return func
return decorator
# 积功德对象
from karma import Karma
karma = Karma()
@command("开始/首页")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("加入游戏群:https://t.me/+SWjgzHmibJc5MGNh")
async def group_message_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
# 只处理群聊消息
if not (update.message and update.message.chat.type in ['group', 'supergroup']):
return
text = update.message.text
user_name = update.message.from_user.first_name
user_id = str(update.message.from_user.id)
server_id = 'tg_' + str(update.message.chat.id)
print(user_id, server_id, user_name)
user = karma.get_user(user_id, server_id, user_name)
if text.startswith('投胎'):
to_buy = text[2:].strip()
await update.message.reply_text(karma.toutai(user, to_buy))
elif text.startswith('连续投胎'):
paras = msg.text.split(' ')
if len(paras) < 2:
await update.message.reply_text('格式:连续投胎 目的地 最大次数,例如:连续投胎 地狱 21\n最大次数可省略,默认为21')
else:
if len(paras) < 3:
paras.append(21)
await update.message.reply_text(karma.batch_toutai(user, paras[1], int(paras[-1])))
elif text == '圆寂':
await update.message.reply_text(karma.circle(user))
elif text.startswith('改词'):
new_word = text[2:].strip()
if new_word == '':
await update.message.reply_text('格式:改词 新词,例如:改词 吃屎')
else:
await update.message.reply_text(karma.change_word(user, new_word))
elif text == '说明':
await update.message.reply_text(r"https://telegra.ph/%E5%8A%9F%E5%BE%B7%E5%A4%A7%E4%BD%9C%E6%88%98%E7%A7%AF%E5%8A%9F%E5%BE%B7%E7%94%B5%E5%AD%90%E6%9C%A8%E9%B1%BC%E6%B8%B8%E6%88%8F%E6%8C%87%E5%8D%97-06-14")
"""
elif text == '删我':
UserTable.find(UserTable.user_id == msg.talker_id).delete()
elif text == '吃屎':
user.items[-1] += 2**100
user.save()
"""
elif text == '功德榜':
await update.message.reply_text(karma.gongdebang(user))
elif text == '果位榜':
await update.message.reply_text(karma.guoweibang(user))
elif text.startswith('显示'):
mode = text[2:].strip()
await update.message.reply_text(karma.show_mode(user, mode))
elif text == user.word:
await update.message.reply_text(karma.jigongde(user))
else:
pass
async def set_menu(application):
await application.bot.set_my_commands(commands)
if __name__ == '__main__':
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, group_message_handler))
application.post_init = set_menu # 设置菜单
application.run_polling()