tickets_plus.bot
This main bot class module for Tickets+.
A module that contains purely the bot class - TicketsPlusBot.
It's a subclass of discord.ext.commands.AutoShardedBot.
No other code is in this module at the moment.
Typical usage example:
from tickets_plus import bot bot_instance = bot.TicketsPlusBot(...)
1"""This main bot class module for Tickets+. 2 3A module that contains purely the bot class - `TicketsPlusBot`. 4It's a subclass of `discord.ext.commands.AutoShardedBot`. 5No other code is in this module at the moment. 6 7Typical usage example: 8 ```py 9 from tickets_plus import bot 10 bot_instance = bot.TicketsPlusBot(...) 11 ``` 12""" 13# License: EPL-2.0 14# SPDX-License-Identifier: EPL-2.0 15# Copyright (c) 2021-present The Tickets+ Contributors 16# This Source Code may also be made available under the following 17# Secondary Licenses when the conditions for such availability set forth 18# in the Eclipse Public License, v. 2.0 are satisfied: GPL-3.0-only OR 19# If later approved by the Initial Contributor, GPL-3.0-or-later. 20 21import logging 22 23import discord 24from discord.ext import commands 25from sqlalchemy.ext import asyncio as sa_asyncio 26 27from tickets_plus import cogs 28from tickets_plus.database import config, const, layer 29 30 31class TicketsPlusBot(commands.AutoShardedBot): 32 """A bot instance that is used to run Tickets+. 33 34 Automatically sharded bot instance used to run Tickets+. 35 This is to allow us to add our own methods and attributes. 36 In general, not much is done in this class. 37 Most of the work is done in the cogs. 38 39 Attributes: 40 stat_confg: The config for the bot. 41 sessions: The database session maker. 42 """ 43 44 stat_confg: config.MiniConfig 45 sessions: sa_asyncio.async_sessionmaker 46 47 def __init__(self, 48 *args, 49 db_engine: sa_asyncio.AsyncEngine, 50 confg: config.MiniConfig = config.MiniConfig(), 51 **kwargs) -> None: 52 """Initializes the bot instance. 53 54 This function is used to initialize the bot instance. 55 We create prep some stuff for the bot to use. 56 57 Args: 58 *args: The arguments to pass to the superclass. 59 db_engine: The database engine. 60 confg: The config for the bot. 61 Defaults to `tickets_plus.statvars.MiniConfig`. 62 **kwargs: The keyword arguments to pass to the superclass. 63 """ 64 super().__init__(*args, **kwargs) 65 self._db_engine = db_engine 66 self.stat_confg = confg 67 self.sessions = sa_asyncio.async_sessionmaker(self._db_engine, expire_on_commit=False) 68 69 async def setup_hook(self) -> None: 70 """Runs just before the bot connects to Discord. 71 72 Sets up the bot for actual use. 73 This is used to load the cogs and sync the database. 74 Generally, this function should not be called manually. 75 """ 76 logging.info("Bot version: %s", const.VERSION) 77 logging.info("Discord.py version: %s", discord.__version__) 78 logging.info("Loading cogs...") 79 for extension in cogs.EXTENSIONS: 80 try: 81 await self.load_extension(extension) 82 except commands.ExtensionError as err: 83 logging.error("Failed to load cog %s: %s", extension, err) 84 logging.info("Finished loading cogs.") 85 86 def get_connection(self) -> layer.OnlineConfig: 87 """Gets a connection from the database pool. 88 89 Grabs a connection from the async session maker. 90 We additionally wrap the connection in a `OnlineConfig` object. 91 This is to allow us to use the OnlineConfig object as a context manager. 92 And to allow for more convenient access to the database. 93 94 Returns: 95 `tickets_plus.layer.OnlineConfig`: The OnlineConfig object. 96 A wrapper for the database connection. 97 """ 98 return layer.OnlineConfig(self, self.sessions()) 99 100 async def close(self) -> None: 101 """Closes the bot. 102 103 This function is used to close the bot. 104 We additionally clean up the database engine/pool. 105 """ 106 logging.info("Closing bot...") 107 await self._db_engine.dispose() 108 return await super().close()
32class TicketsPlusBot(commands.AutoShardedBot): 33 """A bot instance that is used to run Tickets+. 34 35 Automatically sharded bot instance used to run Tickets+. 36 This is to allow us to add our own methods and attributes. 37 In general, not much is done in this class. 38 Most of the work is done in the cogs. 39 40 Attributes: 41 stat_confg: The config for the bot. 42 sessions: The database session maker. 43 """ 44 45 stat_confg: config.MiniConfig 46 sessions: sa_asyncio.async_sessionmaker 47 48 def __init__(self, 49 *args, 50 db_engine: sa_asyncio.AsyncEngine, 51 confg: config.MiniConfig = config.MiniConfig(), 52 **kwargs) -> None: 53 """Initializes the bot instance. 54 55 This function is used to initialize the bot instance. 56 We create prep some stuff for the bot to use. 57 58 Args: 59 *args: The arguments to pass to the superclass. 60 db_engine: The database engine. 61 confg: The config for the bot. 62 Defaults to `tickets_plus.statvars.MiniConfig`. 63 **kwargs: The keyword arguments to pass to the superclass. 64 """ 65 super().__init__(*args, **kwargs) 66 self._db_engine = db_engine 67 self.stat_confg = confg 68 self.sessions = sa_asyncio.async_sessionmaker(self._db_engine, expire_on_commit=False) 69 70 async def setup_hook(self) -> None: 71 """Runs just before the bot connects to Discord. 72 73 Sets up the bot for actual use. 74 This is used to load the cogs and sync the database. 75 Generally, this function should not be called manually. 76 """ 77 logging.info("Bot version: %s", const.VERSION) 78 logging.info("Discord.py version: %s", discord.__version__) 79 logging.info("Loading cogs...") 80 for extension in cogs.EXTENSIONS: 81 try: 82 await self.load_extension(extension) 83 except commands.ExtensionError as err: 84 logging.error("Failed to load cog %s: %s", extension, err) 85 logging.info("Finished loading cogs.") 86 87 def get_connection(self) -> layer.OnlineConfig: 88 """Gets a connection from the database pool. 89 90 Grabs a connection from the async session maker. 91 We additionally wrap the connection in a `OnlineConfig` object. 92 This is to allow us to use the OnlineConfig object as a context manager. 93 And to allow for more convenient access to the database. 94 95 Returns: 96 `tickets_plus.layer.OnlineConfig`: The OnlineConfig object. 97 A wrapper for the database connection. 98 """ 99 return layer.OnlineConfig(self, self.sessions()) 100 101 async def close(self) -> None: 102 """Closes the bot. 103 104 This function is used to close the bot. 105 We additionally clean up the database engine/pool. 106 """ 107 logging.info("Closing bot...") 108 await self._db_engine.dispose() 109 return await super().close()
A bot instance that is used to run Tickets+.
Automatically sharded bot instance used to run Tickets+. This is to allow us to add our own methods and attributes. In general, not much is done in this class. Most of the work is done in the cogs.
Attributes:
- stat_confg: The config for the bot.
- sessions: The database session maker.
48 def __init__(self, 49 *args, 50 db_engine: sa_asyncio.AsyncEngine, 51 confg: config.MiniConfig = config.MiniConfig(), 52 **kwargs) -> None: 53 """Initializes the bot instance. 54 55 This function is used to initialize the bot instance. 56 We create prep some stuff for the bot to use. 57 58 Args: 59 *args: The arguments to pass to the superclass. 60 db_engine: The database engine. 61 confg: The config for the bot. 62 Defaults to `tickets_plus.statvars.MiniConfig`. 63 **kwargs: The keyword arguments to pass to the superclass. 64 """ 65 super().__init__(*args, **kwargs) 66 self._db_engine = db_engine 67 self.stat_confg = confg 68 self.sessions = sa_asyncio.async_sessionmaker(self._db_engine, expire_on_commit=False)
Initializes the bot instance.
This function is used to initialize the bot instance. We create prep some stuff for the bot to use.
Arguments:
- *args: The arguments to pass to the superclass.
- db_engine: The database engine.
- confg: The config for the bot.
Defaults to
tickets_plus.statvars.MiniConfig. - **kwargs: The keyword arguments to pass to the superclass.
70 async def setup_hook(self) -> None: 71 """Runs just before the bot connects to Discord. 72 73 Sets up the bot for actual use. 74 This is used to load the cogs and sync the database. 75 Generally, this function should not be called manually. 76 """ 77 logging.info("Bot version: %s", const.VERSION) 78 logging.info("Discord.py version: %s", discord.__version__) 79 logging.info("Loading cogs...") 80 for extension in cogs.EXTENSIONS: 81 try: 82 await self.load_extension(extension) 83 except commands.ExtensionError as err: 84 logging.error("Failed to load cog %s: %s", extension, err) 85 logging.info("Finished loading cogs.")
Runs just before the bot connects to Discord.
Sets up the bot for actual use. This is used to load the cogs and sync the database. Generally, this function should not be called manually.
87 def get_connection(self) -> layer.OnlineConfig: 88 """Gets a connection from the database pool. 89 90 Grabs a connection from the async session maker. 91 We additionally wrap the connection in a `OnlineConfig` object. 92 This is to allow us to use the OnlineConfig object as a context manager. 93 And to allow for more convenient access to the database. 94 95 Returns: 96 `tickets_plus.layer.OnlineConfig`: The OnlineConfig object. 97 A wrapper for the database connection. 98 """ 99 return layer.OnlineConfig(self, self.sessions())
Gets a connection from the database pool.
Grabs a connection from the async session maker.
We additionally wrap the connection in a OnlineConfig object.
This is to allow us to use the OnlineConfig object as a context manager.
And to allow for more convenient access to the database.
Returns:
tickets_plus.layer.OnlineConfig: The OnlineConfig object. A wrapper for the database connection.
101 async def close(self) -> None: 102 """Closes the bot. 103 104 This function is used to close the bot. 105 We additionally clean up the database engine/pool. 106 """ 107 logging.info("Closing bot...") 108 await self._db_engine.dispose() 109 return await super().close()
Closes the bot.
This function is used to close the bot. We additionally clean up the database engine/pool.