mirror of https://github.com/Mai-with-u/MaiBot.git
commit
c730ed4c57
|
|
@ -1,11 +1,10 @@
|
||||||
FROM nonebot/nb-cli:latest
|
FROM nonebot/nb-cli:latest
|
||||||
WORKDIR /
|
WORKDIR /
|
||||||
RUN apt update && apt install -y git
|
COPY . /MaiMBot/
|
||||||
RUN git clone https://github.com/SengokuCola/MaiMBot
|
|
||||||
WORKDIR /MaiMBot
|
WORKDIR /MaiMBot
|
||||||
RUN mkdir config
|
RUN mkdir config
|
||||||
RUN mv /MaiMBot/env.example /MaiMBot/config/.env \
|
RUN mv env.example config/.env \
|
||||||
&& mv /MaiMBot/src/plugins/chat/bot_config_toml /MaiMBot/config/bot_config.toml
|
&& mv src/plugins/chat/bot_config_toml config/bot_config.toml
|
||||||
RUN ln -s /MaiMBot/config/.env /MaiMBot/.env \
|
RUN ln -s /MaiMBot/config/.env /MaiMBot/.env \
|
||||||
&& ln -s /MaiMBot/config/bot_config.toml /MaiMBot/src/plugins/chat/bot_config.toml
|
&& ln -s /MaiMBot/config/bot_config.toml /MaiMBot/src/plugins/chat/bot_config.toml
|
||||||
RUN pip install -r requirements.txt
|
RUN pip install -r requirements.txt
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,9 @@ PLUGINS=["src2.plugins.chat"]
|
||||||
MONGODB_HOST=127.0.0.1
|
MONGODB_HOST=127.0.0.1
|
||||||
MONGODB_PORT=27017
|
MONGODB_PORT=27017
|
||||||
DATABASE_NAME=MegBot
|
DATABASE_NAME=MegBot
|
||||||
|
MONGODB_USERNAME = "" # 默认空值
|
||||||
|
MONGODB_PASSWORD = "" # 默认空值
|
||||||
|
MONGODB_AUTH_SOURCE = "" # 默认空值
|
||||||
|
|
||||||
#key and url
|
#key and url
|
||||||
CHAT_ANY_WHERE_KEY=
|
CHAT_ANY_WHERE_KEY=
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,24 @@ from typing import Optional
|
||||||
class Database:
|
class Database:
|
||||||
_instance: Optional["Database"] = None
|
_instance: Optional["Database"] = None
|
||||||
|
|
||||||
def __init__(self, host: str, port: int, db_name: str):
|
def __init__(self, host: str, port: int, db_name: str, username: Optional[str] = None, password: Optional[str] = None, auth_source: Optional[str] = None):
|
||||||
self.client = MongoClient(host, port)
|
if username and password:
|
||||||
|
# 如果有用户名和密码,使用认证连接
|
||||||
|
# TODO: 复杂情况直接支持URI吧
|
||||||
|
self.client = MongoClient(host, port, username=username, password=password, authSource=auth_source)
|
||||||
|
else:
|
||||||
|
# 否则使用无认证连接
|
||||||
|
self.client = MongoClient(host, port)
|
||||||
self.db = self.client[db_name]
|
self.db = self.client[db_name]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def initialize(cls, host: str, port: int, db_name: str) -> "Database":
|
def initialize(cls, host: str, port: int, db_name: str, username: Optional[str] = None, password: Optional[str] = None, auth_source: Optional[str] = None) -> "Database":
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
cls._instance = cls(host, port, db_name)
|
cls._instance = cls(host, port, db_name, username, password, auth_source)
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_instance(cls) -> "Database":
|
def get_instance(cls) -> "Database":
|
||||||
if cls._instance is None:
|
if cls._instance is None:
|
||||||
raise RuntimeError("Database not initialized")
|
raise RuntimeError("Database not initialized")
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
host = "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
port = 27017
|
port = 27017
|
||||||
name = "MegBot"
|
name = "MegBot"
|
||||||
|
username = "" # 默认空值
|
||||||
|
password = "" # 默认空值
|
||||||
|
auth_source = "" # 默认空值
|
||||||
|
|
||||||
[bot]
|
[bot]
|
||||||
qq = #填入你的机器人QQ
|
qq = #填入你的机器人QQ
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional, Set
|
||||||
import os
|
import os
|
||||||
from nonebot.log import logger, default_format
|
from nonebot.log import logger, default_format
|
||||||
import logging
|
import logging
|
||||||
import configparser # 添加这行导入
|
import configparser
|
||||||
import tomli # 添加这行导入
|
import tomli
|
||||||
|
|
||||||
# 禁用默认的日志输出
|
# 禁用默认的日志输出
|
||||||
# logger.remove()
|
# logger.remove()
|
||||||
|
|
@ -24,6 +24,9 @@ class BotConfig:
|
||||||
MONGODB_HOST: str = "127.0.0.1"
|
MONGODB_HOST: str = "127.0.0.1"
|
||||||
MONGODB_PORT: int = 27017
|
MONGODB_PORT: int = 27017
|
||||||
DATABASE_NAME: str = "MegBot"
|
DATABASE_NAME: str = "MegBot"
|
||||||
|
MONGODB_USERNAME: Optional[str] = None # 默认空值
|
||||||
|
MONGODB_PASSWORD: Optional[str] = None # 默认空值
|
||||||
|
MONGODB_AUTH_SOURCE: Optional[str] = None # 默认空值
|
||||||
|
|
||||||
BOT_QQ: Optional[int] = None
|
BOT_QQ: Optional[int] = None
|
||||||
BOT_NICKNAME: Optional[str] = None
|
BOT_NICKNAME: Optional[str] = None
|
||||||
|
|
@ -59,7 +62,10 @@ class BotConfig:
|
||||||
config.MONGODB_HOST = db_config.get("host", config.MONGODB_HOST)
|
config.MONGODB_HOST = db_config.get("host", config.MONGODB_HOST)
|
||||||
config.MONGODB_PORT = db_config.get("port", config.MONGODB_PORT)
|
config.MONGODB_PORT = db_config.get("port", config.MONGODB_PORT)
|
||||||
config.DATABASE_NAME = db_config.get("name", config.DATABASE_NAME)
|
config.DATABASE_NAME = db_config.get("name", config.DATABASE_NAME)
|
||||||
|
config.MONGODB_USERNAME = db_config.get("username", config.MONGODB_USERNAME) or None # 空字符串转为 None
|
||||||
|
config.MONGODB_PASSWORD = db_config.get("password", config.MONGODB_PASSWORD) or None # 空字符串转为 None
|
||||||
|
config.MONGODB_AUTH_SOURCE = db_config.get("auth_source", config.MONGODB_AUTH_SOURCE) or None # 空字符串转为 None
|
||||||
|
|
||||||
if "emoji" in toml_dict:
|
if "emoji" in toml_dict:
|
||||||
emoji_config = toml_dict["emoji"]
|
emoji_config = toml_dict["emoji"]
|
||||||
config.EMOJI_CHECK_INTERVAL = emoji_config.get("check_interval", config.EMOJI_CHECK_INTERVAL)
|
config.EMOJI_CHECK_INTERVAL = emoji_config.get("check_interval", config.EMOJI_CHECK_INTERVAL)
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,10 @@ def storage_compress_image(image_data: bytes, max_size: int = 200) -> bytes:
|
||||||
db = Database(
|
db = Database(
|
||||||
host=bot_config.MONGODB_HOST,
|
host=bot_config.MONGODB_HOST,
|
||||||
port=bot_config.MONGODB_PORT,
|
port=bot_config.MONGODB_PORT,
|
||||||
db_name=bot_config.DATABASE_NAME
|
db_name=bot_config.DATABASE_NAME,
|
||||||
|
username=bot_config.MONGODB_USERNAME,
|
||||||
|
password=bot_config.MONGODB_PASSWORD,
|
||||||
|
auth_source=bot_config.MONGODB_AUTH_SOURCE
|
||||||
)
|
)
|
||||||
|
|
||||||
# 检查是否已存在相同哈希值的图片
|
# 检查是否已存在相同哈希值的图片
|
||||||
|
|
|
||||||
|
|
@ -11,10 +11,19 @@ from ...common.database import Database # 使用正确的导入语法
|
||||||
# from src.plugins.schedule.schedule_llm_module import LLMModel
|
# from src.plugins.schedule.schedule_llm_module import LLMModel
|
||||||
# from src.common.database import Database # 使用正确的导入语法
|
# from src.common.database import Database # 使用正确的导入语法
|
||||||
|
|
||||||
|
# 获取当前文件的绝对路径
|
||||||
|
#TODO: 这个好几个地方用需要封装
|
||||||
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
root_dir = os.path.abspath(os.path.join(current_dir, '..', '..', '..'))
|
||||||
|
load_dotenv(os.path.join(root_dir, '.env'))
|
||||||
|
|
||||||
Database.initialize(
|
Database.initialize(
|
||||||
os.getenv("MONGODB_HOST"),
|
host= os.getenv("MONGODB_HOST"),
|
||||||
int(os.getenv("MONGODB_PORT")),
|
port= int(os.getenv("MONGODB_PORT")),
|
||||||
os.getenv("DATABASE_NAME")
|
db_name= os.getenv("DATABASE_NAME"),
|
||||||
|
username= os.getenv("MONGODB_USERNAME"),
|
||||||
|
password= os.getenv("MONGODB_PASSWORD"),
|
||||||
|
auth_source=os.getenv("MONGODB_AUTH_SOURCE")
|
||||||
)
|
)
|
||||||
|
|
||||||
class ScheduleGenerator:
|
class ScheduleGenerator:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue