Skip to content

Commit

Permalink
重构项目结构
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Oct 10, 2023
1 parent 0704f69 commit de2c905
Show file tree
Hide file tree
Showing 50 changed files with 533 additions and 635 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ FROM python:3.11.6-slim-bookworm
COPY . /kmua
WORKDIR /kmua
RUN apt-get update && apt-get install graphviz -y && pip install -r requirements.txt
ENTRYPOINT [ "python","/kmua/bot.py" ]
ENTRYPOINT [ "python","-m","kmua" ]
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
kmua:
image: ghcr.io/krau/kmua-bot:v2
container_name: kmua-v2-main
container_name: kmua-main
restart: always
volumes:
- ./data:/kmua/data # 不要修改此挂载
Expand Down
Empty file added kmua/__init__.py
Empty file.
15 changes: 6 additions & 9 deletions bot.py → kmua/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
from src.dao import db

import pytz
from telegram.constants import UpdateType
Expand All @@ -9,11 +8,11 @@
ApplicationBuilder,
Defaults,
)

from src.callbacks.jobs import refresh_waifu_data
from src.config import settings
from src.handlers import handlers, on_error
from src.logger import logger
from kmua.logger import logger
from kmua.config import settings
import kmua.dao._db as db
from kmua.handlers import handlers, on_error
from kmua.callbacks.jobs import refresh_waifu_data


async def init_data(app: Application):
Expand All @@ -34,7 +33,6 @@ async def init_data(app: Application):
("id", "获取聊天ID"),
]
)
# dao.init_db()
logger.success("started bot")


Expand Down Expand Up @@ -80,5 +78,4 @@ def run():
)


if __name__ == "__main__":
run()
run()
Empty file added kmua/callbacks/__init__.py
Empty file.
11 changes: 6 additions & 5 deletions src/callbacks/chatdata.py → kmua/callbacks/chatdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
Update,
)
from telegram.ext import ContextTypes
from ..common.chat import get_chat_info
from ..logger import logger
from ..service import chat as chat_service

import kmua.common as common
from kmua.logger import logger
import kmua.dao as dao


async def chat_data_manage(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
logger.info(f"chat_data_manage: {chat.title}")
text = get_chat_info(chat)
text = common.get_chat_info(chat)
await chat.send_message(text=text)
# TODO: manage chat data

Expand All @@ -19,4 +20,4 @@ async def chat_title_update(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
logger.info(f"update_chat_title: {chat.title}")
title = chat.title
chat_service.update_chat_title(chat=chat, title=title)
dao.update_chat_title(chat=chat, title=title)
7 changes: 3 additions & 4 deletions src/callbacks/chatinfo.py → kmua/callbacks/chatinfo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from telegram import (
Update,
)
from ..logger import logger
from telegram import Update
from telegram.ext import ContextTypes

from kmua.logger import logger


async def getid(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.info(
Expand Down
21 changes: 8 additions & 13 deletions src/callbacks/chatmember.py → kmua/callbacks/chatmember.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
from telegram import Chat, ChatMember, ChatMemberUpdated, Update
from telegram.ext import ContextTypes

from ..dao.association import delete_association_in_chat
from ..dao.chat import add_chat
from ..dao import db
from ..logger import logger
from ..service.chat import delete_chat_data_and_quotes
from ..common.user import verify_user_can_manage_bot_in_chat
import kmua.common as common
import kmua.dao as dao
from kmua.logger import logger


def extract_status_change(chat_member_update: ChatMemberUpdated):
Expand Down Expand Up @@ -60,7 +57,7 @@ async def track_chats(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non

elif was_member and not is_member:
logger.debug(f"{cause_name} 将bot移出群组 {chat.title}")
delete_chat_data_and_quotes(chat)
dao.delete_chat_data_and_quotes(chat)

elif not was_member and is_member:
logger.debug(f"{cause_name} 将bot添加到频道 {chat.title}")
Expand All @@ -77,7 +74,7 @@ async def track_chats(update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non
async def on_member_left(update: Update, context: ContextTypes.DEFAULT_TYPE):
left_user = update.effective_message.left_chat_member
logger.debug(f"{left_user.full_name} 退出了群聊 {update.effective_chat.title}")
delete_association_in_chat(update.effective_chat, left_user)
dao.delete_association_in_chat(update.effective_chat, left_user)


async def on_member_join(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand All @@ -86,7 +83,7 @@ async def on_member_join(update: Update, context: ContextTypes.DEFAULT_TYPE):
logger.debug(f"{joined_user} 加入了群聊 {chat.title} ")
if joined_user.is_bot:
return
db_chat = add_chat(chat)
db_chat = dao.add_chat(chat)
greet = db_chat.greet
if greet:
greet = greet.format(
Expand All @@ -105,13 +102,11 @@ async def set_greet(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat = update.effective_chat
logger.info(f"[{chat.title}]({user.name}) <set_greet>")
message = update.effective_message
if not await verify_user_can_manage_bot_in_chat(user, chat, update, context):
if not await common.verify_user_can_manage_bot_in_chat(user, chat, update, context):
await message.reply_text("你没有权限哦")
return
greet = " ".join(context.args)
db_chat = add_chat(chat)
db_chat.greet = greet
db.commit()
dao.update_chat_greet(chat, greet)
if greet:
await message.reply_text("设置成功")
else:
Expand Down
8 changes: 4 additions & 4 deletions src/callbacks/friendship.py → kmua/callbacks/friendship.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from telegram import Update
from telegram.ext import ContextTypes

from ..common.utils import ohayo_word, oyasumi_word
from ..logger import logger
from kmua.logger import logger
from .jobs import send_message
import kmua.common as common


async def ohayo(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand All @@ -18,7 +18,7 @@ async def ohayo(update: Update, context: ContextTypes.DEFAULT_TYPE):
when=random.randint(22, 25) * random.randint(3500, 3600),
data={
"chat_id": update.effective_user.id,
"text": random.choice(ohayo_word),
"text": random.choice(common.ohayo_word),
},
name=f"ohayo_{update.effective_user.id}",
)
Expand All @@ -34,7 +34,7 @@ async def oyasumi(update: Update, context: ContextTypes.DEFAULT_TYPE):
when=random.randint(22, 25) * random.randint(3500, 3600),
data={
"chat_id": update.effective_user.id,
"text": random.choice(oyasumi_word),
"text": random.choice(common.oyasumi_word),
},
name=f"oyasumi_{update.effective_user.id}",
)
10 changes: 5 additions & 5 deletions src/callbacks/help.py → kmua/callbacks/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
)
from telegram.ext import ContextTypes

from ..logger import logger
from ..common.message import message_recorder
import kmua.common as common
from kmua.logger import logger


async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand Down Expand Up @@ -48,10 +48,10 @@ async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
]
]
)
sent_message = await context.bot.send_message(
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=help_text,
reply_markup=help_markup,
)
message_recorder(update, context)
logger.info(f"Bot: {sent_message.text}")
common.message_recorder(update, context)
logger.info("Bot: <help text>")
9 changes: 4 additions & 5 deletions src/callbacks/jobs.py → kmua/callbacks/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

from telegram.ext import ContextTypes

from ..dao.chat import get_all_chats
from ..logger import logger
from ..service.waifu import refresh_all_waifu_data
from kmua.logger import logger
import kmua.dao as dao
from .waifu import send_waifu_graph


Expand All @@ -13,7 +12,7 @@ async def refresh_waifu_data(context: ContextTypes.DEFAULT_TYPE):
try:
context.bot_data["refeshing_waifu_data"] = True
await asyncio.gather(
*(send_waifu_graph(chat, context) for chat in get_all_chats())
*(send_waifu_graph(chat, context) for chat in dao.get_all_chats())
)
except Exception as err:
logger.error(
Expand All @@ -22,7 +21,7 @@ async def refresh_waifu_data(context: ContextTypes.DEFAULT_TYPE):
raise err
finally:
await asyncio.sleep(3)
await refresh_all_waifu_data()
await dao.refresh_all_waifu_data()
logger.success("数据已刷新: waifu_data")
context.bot_data["refeshing_waifu_data"] = False

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
from telegram.constants import ChatAction
from telegram.ext import ContextTypes
from zhconv import convert

import asyncio
from ..common.utils import word_dict
from ..logger import logger
from ..common.message import message_recorder
from .friendship import ohayo, oyasumi

import kmua.common as common
from kmua.logger import logger
from telegram.error import BadRequest
from .friendship import ohayo, oyasumi



async def keyword_reply(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand All @@ -28,7 +30,7 @@ async def keyword_reply(update: Update, context: ContextTypes.DEFAULT_TYPE):
)
sent_message = None
all_resplist = []
for keyword, resplist in word_dict.items():
for keyword, resplist in common.word_dict.items():
if keyword in message_text:
all_resplist.extend(resplist)
if keyword == "早":
Expand All @@ -42,7 +44,7 @@ async def keyword_reply(update: Update, context: ContextTypes.DEFAULT_TYPE):
allow_sending_without_reply=False,
)
logger.info("Bot: " + sent_message.text)
message_recorder(update, context)
common.message_recorder(update, context)
if sent_message:
await asyncio.sleep(30)
source_message = sent_message.reply_to_message
Expand Down
Loading

0 comments on commit de2c905

Please sign in to comment.