Skip to content

Commit

Permalink
chore: function structure
Browse files Browse the repository at this point in the history
  • Loading branch information
pplulee committed May 2, 2024
1 parent 235ebbd commit d00e1b6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 46 deletions.
2 changes: 1 addition & 1 deletion db_migrate/20240502_response_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
def upgrade(db_path):
with sqlite3.connect(db_path) as conn:
db_cursor = conn.cursor()
db_cursor.execute("ALTER TABLE auto_response ADD COLUMN action_handle BOOLEAN DEFAULT 0")
db_cursor.execute("ALTER TABLE auto_response ADD COLUMN topic_action BOOLEAN DEFAULT 0")
conn.commit()
Binary file modified locale/zh_CN/LC_MESSAGES/BetterForward.mo
Binary file not shown.
91 changes: 68 additions & 23 deletions locale/zh_CN/LC_MESSAGES/BetterForward.po
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
msgid ""
msgstr ""
"Project-Id-Version: BetterForward\n"
"POT-Creation-Date: 2024-04-30 10:49+0100\n"
"PO-Revision-Date: 2024-04-30 10:50+0100\n"
"POT-Creation-Date: 2024-05-02 23:21+0100\n"
"PO-Revision-Date: 2024-05-02 23:21+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: zh_CN\n"
Expand All @@ -13,81 +13,126 @@ msgstr ""
"X-Poedit-Basepath: ../../..\n"
"X-Poedit-SearchPath-0: main.py\n"

#: main.py:38
#: main.py:39
msgid "Starting BetterForward..."
msgstr "正在启动BetterForward……"

#: main.py:48
#: main.py:49
msgid "Terminate a thread"
msgstr "删除一个会话"

#: main.py:65
msgid "No auto response found"
msgstr "您还未设置自动回复"
#: main.py:55
msgid ""
"Invalid command\n"
"Correct usage:\n"
"`/auto_response set <key> <value> <topic_action(0/1)>`\n"
"`/auto_response delete <key>`\n"
"`/auto_response list`"
msgstr ""
"指令格式不正确\n"
"使用方法:\n"
"`/auto_response set <key> <value> <topic_action(0/1)>`\n"
"`/auto_response delete <key>`\n"
"`/auto_response list`"

#: main.py:71
msgid "Auto response list"
msgstr "自动回复列表"

#: main.py:74
msgid ""
"Trigger: {}\n"
"Response: {}\n"
"Forward message: {}\n"
msgstr ""
"关键词:{}\n"
"回复:{}\n"
"转发消息:{}\n"

#: main.py:83
#: main.py:75
msgid "Enable"
msgstr "启用"

#: main.py:76
msgid "Disable"
msgstr "禁用"

#: main.py:95
msgid "Auto response set"
msgstr "自动回复设置成功"

#: main.py:92
#: main.py:102
msgid "Auto response deleted"
msgstr "已删除自动回复"

#: main.py:94
#: main.py:104
msgid "Invalid operation"
msgstr "无效的操作"

#: main.py:119
#: main.py:121
msgid "Upgrading database to version {}"
msgstr "升级到数据库版本{}"

#: main.py:131
msgid "Terminating thread"
msgstr "删除会话"

#: main.py:138
#: main.py:151
msgid "Cannot terminate main thread"
msgstr "无法删除默认会话"

#: main.py:143
#: main.py:156
msgid "Thread terminated"
msgstr "会话已被删除"

#: main.py:145
#: main.py:158 main.py:159
msgid "Failed to terminate the thread"
msgstr "无法删除该会话"

#: main.py:154
#: main.py:169
msgid "Received message from {}, content: {}"
msgstr "收到来自{}的消息,内容:{}"

#: main.py:161
#: main.py:183
msgid "Creating a new thread for user {}"
msgstr "与用户{}新建话题"

#: main.py:171
#: main.py:193
msgid "Not set"
msgstr "未设置"

#: main.py:186
#: main.py:204
msgid "[Auto Response]"
msgstr "[自动回复]"

#: main.py:208
#: main.py:226
msgid "Unsupported message type"
msgstr "不支持的消息种类"

#: main.py:210
#: main.py:228
msgid "Chat not found, please remove this topic manually"
msgstr "未找到对应会话,请手动删除当前话题"

#: main.py:221 main.py:222
#: main.py:241 main.py:242
msgid "Bot doesn't have {} permission"
msgstr "Bot缺少{}权限"

#: main.py:224
#: main.py:243
msgid "Bot started successfully"
msgstr "Bot启动成功"

#: main.py:229
#: main.py:248
msgid "Token or group ID is empty"
msgstr "Token或群组ID为空"

#~ msgid "True"
#~ msgstr "启用"

#~ msgid "False"
#~ msgstr "禁用"

#~ msgid "No auto response found"
#~ msgstr "您还未设置自动回复"

#~ msgid "I don't have permission to delete messages"
#~ msgstr "我没有权限删除消息"
52 changes: 30 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,53 @@ def __init__(self, bot_token: str, group_id: str, db_path: str = "./data/storage
self.check_permission()
self.bot.infinity_polling(skip_pending=True, timeout=30)

def get_auto_response_help(self):
return _("Invalid command\n"
"Correct usage:\n"
"`/auto_response set <key> <value> <topic_action(0/1)>`\n"
"`/auto_response delete <key>`\n"
"`/auto_response list`")

def manage_auto_response(self, message: Message):
if message.chat.id == self.group_id:
if len((msg_split := message.text.split(" "))) < 2:
self.bot.reply_to(message, "Invalid command\n"
"Correct usage:```\n"
"/auto_response <set/delete/list> [key] [value] [topic_action(0/1)]```", parse_mode="Markdown")
self.bot.reply_to(message, self.get_auto_response_help(), parse_mode="Markdown")
return
with sqlite3.connect(self.db_path) as db:
db_cursor = db.cursor()
if msg_split[1] == "list":
result = db_cursor.execute("SELECT topic_action, key, value FROM auto_response")
response = "\n".join([f"{row[0]}: {row[1]}" for row in result.fetchall()])
self.bot.reply_to(message, response if response else _("No auto response found"))
return
topic_action = msg_split[4]
key = msg_split[2]
value = " ".join(msg_split[3:])
match msg_split[1]:
case "list":
result = db_cursor.execute("SELECT key, value, topic_action FROM auto_response")
content = _("Auto response list")
content += "\n" + "-" * 20 + "\n"
for row in result.fetchall():
content += _("Trigger: {}\nResponse: {}\nForward message: {}\n").format(row[0], row[1],
_("Enable") if row[2]
else _("Disable"))
content += "-" * 20 + "\n"
self.bot.reply_to(message, content)
return
case "set":
if len(msg_split) != 5:
self.bot.reply_to(message, "Invalid command\n"
"Correct usage:```\n"
"/auto_response set <key> <value> <topic_action(0/1)>```",
parse_mode="Markdown")
if len(msg_split) not in [4, 5]:
self.bot.reply_to(message, self.get_auto_response_help(), parse_mode="Markdown")
return
key = msg_split[2]
value = msg_split[3]
topic_action = int(msg_split[4]) if len(msg_split) == 5 else 0
# Check if key exists
db_cursor.execute("SELECT key FROM auto_response WHERE key = ?", (key,))
if db_cursor.fetchone() is not None:
db_cursor.execute("UPDATE auto_response SET value = ?, topic_action = ? WHERE key = ?", (value, topic_action, key))
db_cursor.execute("UPDATE auto_response SET value = ?, topic_action = ? WHERE key = ?",
(value, topic_action, key))
else:
db_cursor.execute("INSERT INTO auto_response (key, value, topic_action) VALUES (?, ?, ?)", (key, value, topic_action))
db_cursor.execute("INSERT INTO auto_response (key, value, topic_action) VALUES (?, ?, ?)",
(key, value, topic_action))
self.bot.reply_to(message, _("Auto response set"))
case "delete":
if len(msg_split) != 3:
self.bot.reply_to(message, "Invalid command\n"
"Correct usage:```\n"
"/auto_response delete <key>```",
parse_mode="Markdown")
self.bot.reply_to(message, self.get_auto_response_help(), parse_mode="Markdown")
return
key = msg_split[2]
db_cursor.execute("DELETE FROM auto_response WHERE key = ?", (key,))
self.bot.reply_to(message, _("Auto response deleted"))
case _:
Expand Down

0 comments on commit d00e1b6

Please sign in to comment.