Skip to content

Commit

Permalink
♻️ PaiGram V4
Browse files Browse the repository at this point in the history
Co-authored-by: luoshuijs <luoshuijs@outlook.com>
Co-authored-by: Karako <karakohear@gmail.com>
Co-authored-by: xtaodada <xtao@xtaolink.cn>
  • Loading branch information
3 people authored Mar 14, 2023
1 parent baceace commit 233e7ab
Show file tree
Hide file tree
Showing 246 changed files with 8,962 additions and 5,893 deletions.
32 changes: 19 additions & 13 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# debug 开关
DEBUG=false

AUTO_RELOAD=false
RELOAD_DELAY=0.25
RELOAD_DIRS=[]
RELOAD_INCLUDE=[]
RELOAD_EXCLUDE=[]

# MySQL
DB_HOST=127.0.0.1
DB_PORT=3306
Expand All @@ -17,14 +23,14 @@ REDIS_PASSWORD=""
# 联系 https://t.me/BotFather 使用 /newbot 命令创建机器人并获取 token
BOT_TOKEN="xxxxxxx"

# bot 管理员
ADMINS=[{ "username": "", "user_id": -1 }]
# bot 所有者
OWNER=0

# 记录错误并发送消息通知开发人员 可选配置项
# ERROR_NOTIFICATION_CHAT_ID=chat_id

# 文章推送群组 可选配置项
# CHANNELS=[{ "name": "", "chat_id": 1}]
# CHANNELS=[]

# 是否允许机器人邀请到其他群 默认不允许 如果允许 可以允许全部人或有认证选项 可选配置项
# JOIN_GROUPS = "NO_ALLOW"
Expand All @@ -33,20 +39,20 @@ ADMINS=[{ "username": "", "user_id": -1 }]
# VERIFY_GROUPS=[]

# logger 配置 可选配置项
LOGGER_NAME="TGPaimon"
# LOGGER_NAME="TGPaimon"
# 打印时的宽度
LOGGER_WIDTH=180
# LOGGER_WIDTH=180
# log 文件存放目录
LOGGER_LOG_PATH="logs"
# LOGGER_LOG_PATH="logs"
# log 时间格式,参考 datetime.strftime
LOGGER_TIME_FORMAT="[%Y-%m-%d %X]"
# LOGGER_TIME_FORMAT="[%Y-%m-%d %X]"
# log 高亮关键词
LOGGER_RENDER_KEYWORDS=["BOT"]
# LOGGER_RENDER_KEYWORDS=["BOT"]
# traceback 相关配置
LOGGER_TRACEBACK_MAX_FRAMES=20
LOGGER_LOCALS_MAX_DEPTH=0
LOGGER_LOCALS_MAX_LENGTH=10
LOGGER_LOCALS_MAX_STRING=80
# LOGGER_TRACEBACK_MAX_FRAMES=20
# LOGGER_LOCALS_MAX_DEPTH=0
# LOGGER_LOCALS_MAX_LENGTH=10
# LOGGER_LOCALS_MAX_STRING=80
# 可被 logger 打印的 record 的名称(默认包含了 LOGGER_NAME )
LOGGER_FILTERED_NAMES=["uvicorn","ErrorPush","ApiHelper"]

Expand Down Expand Up @@ -77,7 +83,7 @@ LOGGER_FILTERED_NAMES=["uvicorn","ErrorPush","ApiHelper"]
# ENKA_NETWORK_API_AGENT=""

# Web Server
# 目前只用于预览模板,仅开发环境启动
# WEB_SWITCH=False # 是否开启
# WEB_URL=http://localhost:8080/
# WEB_HOST=localhost
# WEB_PORT=8080
Expand Down
54 changes: 54 additions & 0 deletions .github/workflows/integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Integration Test

on:
push:
branches:
- main
paths:
- 'tests/integration/**'
pull_request:
types: [ opened, synchronize ]
paths:
- 'core/services/**'
- 'core/dependence/**'
- 'tests/integration/**'

jobs:
pytest:
name: pytest
runs-on: ubuntu-latest
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: integration_test
MYSQL_ROOT_PASSWORD: 123456test
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
redis:
image: redis
ports:
- 6379:6379
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: 3.11
- name: Setup integration test environment
run: cp tests/integration/.env.example .env && cp tests/integration/.env.example tests/integration/.env
- name: Create venv
run: |
pip install --upgrade pip
python3 -m venv venv
- name: Install requirements
run: |
source venv/bin/activate
python3 -m pip install --upgrade poetry
python3 -m poetry install --extras all
- name: Run test
run: |
source venv/bin/activate
python3 -m pytest tests/integration
21 changes: 9 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
name: test
name: Test modules

on:
push:
branches:
- main
paths:
- 'tests/**'
- 'tests/unit/**'
pull_request:
types: [ opened, synchronize ]
paths:
- 'modules/apihelper/**'
- 'modules/wiki/**'
- 'tests/**'
schedule:
- cron: '0 4 * * 3'
- 'tests/unit/**'

jobs:
pytest:
Expand All @@ -22,16 +20,15 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
python-version: [ '3.10' ]
os: [ ubuntu-latest, windows-latest ]
experimental: [ false ]
fail-fast: False
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
python-version: 3.11
- name: restore or create a python virtualenv
id: cache
uses: syphar/restore-virtualenv@v1.2
Expand All @@ -45,4 +42,4 @@ jobs:
poetry install --extras test
- name: Test with pytest
run: |
python -m pytest
python -m pytest tests/unit
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ plugins/private
.pytest_cache

### mtp ###
paimon.session
PaimonBot.session
PaimonBot.session-journal
paigram.session
paigram.session-journal
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<h1 align="center">PaiGram</h1>

<div align="center">
<img src="https://img.shields.io/badge/python-3.8%2B-blue" alt="">
<div align="center">·<img src="https://img.shields.io/badge/python-3.11%2B-blue" alt="">
<img src="https://img.shields.io/badge/works%20on-my%20machine-brightgreen" alt="">
<img src="https://img.shields.io/badge/status-%E5%92%95%E5%92%95%E5%92%95-blue" alt="">
<a href="https://black.readthedocs.io/en/stable/index.html"><img src="https://img.shields.io/badge/code%20style-black-000000.svg" alt="code_style" /></a>
Expand All @@ -19,7 +18,7 @@

## 环境需求

- Python 3.8+
- Python 3.11+
- MySQL
- Redis

Expand Down
27 changes: 10 additions & 17 deletions alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@
from typing import Iterator

from alembic import context
from sqlalchemy import (
engine_from_config,
pool,
)
from sqlalchemy import engine_from_config, pool
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import AsyncEngine
from sqlmodel import SQLModel

from utils.const import (
CORE_DIR,
PLUGIN_DIR,
PROJECT_ROOT,
)
from core.config import config as BotConfig
from utils.const import CORE_DIR, PLUGIN_DIR, PROJECT_ROOT
from utils.log import logger

# this is the Alembic Config object, which provides
Expand All @@ -28,7 +22,7 @@
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
fileConfig(config.config_file_name) # skipcq: PY-A6006


def scan_models() -> Iterator[str]:
Expand All @@ -46,7 +40,7 @@ def import_models():
try:
import_module(pkg) # 导入 models
except Exception as e: # pylint: disable=W0703
logger.error(f'在导入文件 "{pkg}" 的过程中遇到了错误: \n[red bold]{type(e).__name__}: {e}[/]')
logger.error("在导入文件 %s 的过程中遇到了错误: \n[red bold]%s: %s[/]", pkg, type(e).__name__, e, extra={"markup": True})


# register our models for alembic to auto-generate migrations
Expand All @@ -61,14 +55,13 @@ def import_models():

# here we allow ourselves to pass interpolation vars to alembic.ini
# from the application config module
from core.config import config as botConfig

section = config.config_ini_section
config.set_section_option(section, "DB_HOST", botConfig.mysql.host)
config.set_section_option(section, "DB_PORT", str(botConfig.mysql.port))
config.set_section_option(section, "DB_USERNAME", botConfig.mysql.username)
config.set_section_option(section, "DB_PASSWORD", botConfig.mysql.password)
config.set_section_option(section, "DB_DATABASE", botConfig.mysql.database)
config.set_section_option(section, "DB_HOST", BotConfig.mysql.host)
config.set_section_option(section, "DB_PORT", str(BotConfig.mysql.port))
config.set_section_option(section, "DB_USERNAME", BotConfig.mysql.username)
config.set_section_option(section, "DB_PASSWORD", BotConfig.mysql.password)
config.set_section_option(section, "DB_DATABASE", BotConfig.mysql.database)


def run_migrations_offline() -> None:
Expand Down
35 changes: 19 additions & 16 deletions alembic/versions/9e9a36470cd5_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@
Create Date: 2022-09-01 16:55:20.372560
"""
from alembic import op
from base64 import b64decode

import sqlalchemy as sa
import sqlmodel

from alembic import op

# revision identifiers, used by Alembic.
revision = "9e9a36470cd5"
down_revision = None
branch_labels = None
depends_on = None
old_cookies_database_name1 = b64decode("bWlob3lvX2Nvb2tpZXM=").decode()
old_cookies_database_name2 = b64decode("aG95b3ZlcnNlX2Nvb2tpZXM=").decode()


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"question",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("text", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
sa.Column("text", sqlmodel.AutoString(), nullable=True),
sa.PrimaryKeyConstraint("id"),
mysql_charset="utf8mb4",
mysql_collate="utf8mb4_general_ci",
Expand All @@ -35,7 +38,7 @@ def upgrade() -> None:
nullable=True,
),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.BigInteger(), nullable=False),
sa.Column("yuanshen_uid", sa.Integer(), nullable=True),
sa.Column("genshin_uid", sa.Integer(), nullable=True),
sa.PrimaryKeyConstraint("id"),
Expand All @@ -46,7 +49,7 @@ def upgrade() -> None:
op.create_table(
"admin",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.BigInteger(), nullable=False),
sa.ForeignKeyConstraint(
["user_id"],
["user.user_id"],
Expand All @@ -60,7 +63,7 @@ def upgrade() -> None:
sa.Column("question_id", sa.Integer(), nullable=True),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("is_correct", sa.Boolean(), nullable=True),
sa.Column("text", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
sa.Column("text", sqlmodel.AutoString(), nullable=True),
sa.ForeignKeyConstraint(
["question_id"],
["question.id"],
Expand All @@ -72,7 +75,7 @@ def upgrade() -> None:
mysql_collate="utf8mb4_general_ci",
)
op.create_table(
"hoyoverse_cookies",
old_cookies_database_name2,
sa.Column("cookies", sa.JSON(), nullable=True),
sa.Column(
"status",
Expand All @@ -85,7 +88,7 @@ def upgrade() -> None:
nullable=True,
),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("user_id", sa.BigInteger(), nullable=True),
sa.ForeignKeyConstraint(
["user_id"],
["user.user_id"],
Expand All @@ -95,7 +98,7 @@ def upgrade() -> None:
mysql_collate="utf8mb4_general_ci",
)
op.create_table(
"mihoyo_cookies",
old_cookies_database_name1,
sa.Column("cookies", sa.JSON(), nullable=True),
sa.Column(
"status",
Expand All @@ -108,7 +111,7 @@ def upgrade() -> None:
nullable=True,
),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("user_id", sa.BigInteger(), nullable=True),
sa.ForeignKeyConstraint(
["user_id"],
["user.user_id"],
Expand All @@ -119,6 +122,9 @@ def upgrade() -> None:
)
op.create_table(
"sign",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.BigInteger(), nullable=False),
sa.Column("chat_id", sa.BigInteger(), nullable=True),
sa.Column(
"time_created",
sa.DateTime(timezone=True),
Expand All @@ -140,14 +146,11 @@ def upgrade() -> None:
),
nullable=True,
),
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("chat_id", sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(
["user_id"],
["user.user_id"],
),
sa.PrimaryKeyConstraint("id"),
sa.PrimaryKeyConstraint("id", "user_id"),
mysql_charset="utf8mb4",
mysql_collate="utf8mb4_general_ci",
)
Expand All @@ -157,8 +160,8 @@ def upgrade() -> None:
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("sign")
op.drop_table("mihoyo_cookies")
op.drop_table("hoyoverse_cookies")
op.drop_table(old_cookies_database_name1)
op.drop_table(old_cookies_database_name2)
op.drop_table("answer")
op.drop_table("admin")
op.drop_table("user")
Expand Down
Loading

0 comments on commit 233e7ab

Please sign in to comment.