|
18 | 18 |
|
19 | 19 | from aiohttp import ClientSession |
20 | 20 | from emoji import UNICODE_EMOJI |
21 | | -from motor.motor_asyncio import AsyncIOMotorClient |
22 | | -from pymongo.errors import ConfigurationError |
23 | 21 |
|
24 | 22 | from pkg_resources import parse_version |
25 | 23 |
|
|
32 | 30 | pass |
33 | 31 |
|
34 | 32 | from core import checks |
35 | | -from core.clients import ApiClient, PluginDatabaseClient |
| 33 | +from core.clients import ApiClient, PluginDatabaseClient, MongoDBClient |
36 | 34 | from core.config import ConfigManager |
37 | 35 | from core.utils import human_join, normalize_alias |
38 | 36 | from core.models import PermissionLevel, SafeFormatter, getLogger, configure_logging |
@@ -72,22 +70,7 @@ def __init__(self): |
72 | 70 | self.log_file_name = os.path.join(temp_dir, f"{self.token.split('.')[0]}.log") |
73 | 71 | self._configure_logging() |
74 | 72 |
|
75 | | - mongo_uri = self.config["mongo_uri"] |
76 | | - if mongo_uri is None: |
77 | | - logger.critical("A Mongo URI is necessary for the bot to function.") |
78 | | - raise RuntimeError |
79 | | - |
80 | | - try: |
81 | | - self.db = AsyncIOMotorClient(mongo_uri).modmail_bot |
82 | | - except ConfigurationError as e: |
83 | | - logger.critical( |
84 | | - "Your MONGO_URI might be copied wrong, try re-copying from the source again. " |
85 | | - "Otherwise noted in the following message:" |
86 | | - ) |
87 | | - logger.critical(e) |
88 | | - sys.exit(0) |
89 | | - |
90 | | - self.plugin_db = PluginDatabaseClient(self) |
| 73 | + self.plugin_db = PluginDatabaseClient(self) # Deprecated |
91 | 74 | self.startup() |
92 | 75 |
|
93 | 76 | @property |
@@ -158,7 +141,11 @@ def session(self) -> ClientSession: |
158 | 141 | @property |
159 | 142 | def api(self) -> ApiClient: |
160 | 143 | if self._api is None: |
161 | | - self._api = ApiClient(self) |
| 144 | + if self.config["database_type"].lower() == "mongodb": |
| 145 | + self._api = MongoDBClient(self) |
| 146 | + else: |
| 147 | + logger.critical("Invalid database type.") |
| 148 | + raise RuntimeError |
162 | 149 | return self._api |
163 | 150 |
|
164 | 151 | async def get_prefix(self, message=None): |
@@ -376,37 +363,16 @@ def command_perm(self, command_name: str) -> PermissionLevel: |
376 | 363 |
|
377 | 364 | async def on_connect(self): |
378 | 365 | try: |
379 | | - await self.validate_database_connection() |
| 366 | + await self.api.validate_database_connection() |
380 | 367 | except Exception: |
381 | 368 | logger.debug("Logging out due to failed database connection.") |
382 | 369 | return await self.logout() |
383 | 370 |
|
384 | 371 | logger.debug("Connected to gateway.") |
385 | 372 | await self.config.refresh() |
386 | | - await self.setup_indexes() |
| 373 | + await self.api.setup_indexes() |
387 | 374 | self._connected.set() |
388 | 375 |
|
389 | | - async def setup_indexes(self): |
390 | | - """Setup text indexes so we can use the $search operator""" |
391 | | - coll = self.db.logs |
392 | | - index_name = "messages.content_text_messages.author.name_text_key_text" |
393 | | - |
394 | | - index_info = await coll.index_information() |
395 | | - |
396 | | - # Backwards compatibility |
397 | | - old_index = "messages.content_text_messages.author.name_text" |
398 | | - if old_index in index_info: |
399 | | - logger.info("Dropping old index: %s", old_index) |
400 | | - await coll.drop_index(old_index) |
401 | | - |
402 | | - if index_name not in index_info: |
403 | | - logger.info('Creating "text" index for logs collection.') |
404 | | - logger.info("Name: %s", index_name) |
405 | | - await coll.create_index( |
406 | | - [("messages.content", "text"), ("messages.author.name", "text"), ("key", "text")] |
407 | | - ) |
408 | | - logger.debug("Successfully configured and verified database indexes.") |
409 | | - |
410 | 376 | async def on_ready(self): |
411 | 377 | """Bot startup, sets uptime.""" |
412 | 378 |
|
@@ -1213,37 +1179,6 @@ async def on_command_error(self, context, exception): |
1213 | 1179 | else: |
1214 | 1180 | logger.error("Unexpected exception:", exc_info=exception) |
1215 | 1181 |
|
1216 | | - async def validate_database_connection(self): |
1217 | | - try: |
1218 | | - await self.db.command("buildinfo") |
1219 | | - except Exception as exc: |
1220 | | - logger.critical("Something went wrong while connecting to the database.") |
1221 | | - message = f"{type(exc).__name__}: {str(exc)}" |
1222 | | - logger.critical(message) |
1223 | | - |
1224 | | - if "ServerSelectionTimeoutError" in message: |
1225 | | - logger.critical( |
1226 | | - "This may have been caused by not whitelisting " |
1227 | | - "IPs correctly. Make sure to whitelist all " |
1228 | | - "IPs (0.0.0.0/0) https://i.imgur.com/mILuQ5U.png" |
1229 | | - ) |
1230 | | - |
1231 | | - if "OperationFailure" in message: |
1232 | | - logger.critical( |
1233 | | - "This is due to having invalid credentials in your MONGO_URI. " |
1234 | | - "Remember you need to substitute `<password>` with your actual password." |
1235 | | - ) |
1236 | | - logger.critical( |
1237 | | - "Be sure to URL encode your username and password (not the entire URL!!), " |
1238 | | - "https://www.urlencoder.io/, if this issue persists, try changing your username and password " |
1239 | | - "to only include alphanumeric characters, no symbols." |
1240 | | - "" |
1241 | | - ) |
1242 | | - raise |
1243 | | - else: |
1244 | | - logger.debug("Successfully connected to the database.") |
1245 | | - logger.line("debug") |
1246 | | - |
1247 | 1182 | async def post_metadata(self): |
1248 | 1183 | info = await self.application_info() |
1249 | 1184 |
|
|
0 commit comments