Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HartexBeta(commands.AutoShardedBot):
- """
- Class: HartexBeta
- Represents the HarTex Beta bot.
- Inherits: commands.AutoShardedBot
- """
- def __init__(self):
- super(HartexBeta, self).__init__(command_prefix="hb.", help_command=None, status=discord.Status.dnd,
- activity=discord.Activity(name="in development",
- type=discord.ActivityType.playing),
- case_insensitive=True)
- self.start_time = datetime.utcnow()
- self.version = __version_format__(1, 0, 0)
- self.shard_count = 1
- async def on_ready(self):
- """
- Asynchronous Method: on_ready
- The method is activated when the bot is online.
- :return:
- """
- print(f"HarTex Beta >> HarTex Beta is online!")
- print(await self.connect_config_database())
- async def on_command(self, context: commands.Context):
- """
- Asynchronous Method: on_command
- The method is activated when a command is executed.
- :param context: The command context.
- :return:
- """
- print(f"HarTex Beta >> Command '{context.command}' is executed by {context.author} in {context.guild}.")
- async def on_guild_join(self, guild: discord.Guild):
- """
- Asynchronous Method: on_guild_join
- This method is activated when the bot joins a Discord guild.
- :param guild: The guild the bot joined.
- :return:
- """
- print(f"HarTex Beta >> The bot is added to {guild.name}(ID: {guild.id}).")
- try:
- cursor = await self.cursor_config
- await cursor.execute(sql.SQL('select * from "HarTexBetaConfig".public.guilds'))
- guilds = await cursor.fetchall()
- for g in guilds:
- if guild.id == g[0]:
- pass
- else:
- continue
- else:
- await guild.leave()
- except Exception as error:
- print(f"HarTex Beta >> Failed to fetch guilds. Error: {error}")
- async def on_guild_remove(self, guild: discord.Guild):
- """
- Asynchronous Method: on_guild_remove
- This method is activated when the bot is removed from a Discord guild.
- :param guild: The guild the bot is removed from.
- :return:
- """
- print(f"HarTex Beta >> The bot is removed from {guild.name}(ID: {guild.id})")
- async def on_command_completion(self, ctx: discord.ext.commands.Context):
- """
- Asynchronous Method: on_command_completion
- This method is activated when a command is completed without errors.
- :param ctx: The command context.
- :return:
- """
- (await self.config_connection).close()
- def run_hartexbeta(self, token: str):
- """
- Method: run_hartexbeta
- Runs the bot.
- :param token: The bot token.
- :return:
- """
- super().run(token)
- async def connect_config_database(self) -> aiopg.Connection:
- """
- Asynchronous Method: connect_config_database
- Connects to the bot database.
- :return:
- """
- try:
- with open("core/database_credentials.yaml", "r") as yaml_reader:
- file = yaml.safe_load(yaml_reader)
- connection = await aiopg.connect(
- database=file['database_credentials']['hartex_beta_config']['database'],
- user=file['database_credentials']['hartex_beta_config']['user'],
- password=file['database_credentials']['hartex_beta_config']['password'],
- host=file['database_credentials']['hartex_beta_config']['host'],
- port=file['database_credentials']['hartex_beta_config']['port']
- )
- return connection
- except Exception as exception:
- print(f"HarTex Beta >> An error occurred when trying to connect to database HarTexBetaConfig: {exception}")
- async def close_hartexbeta(self):
- """
- Asynchronous Method: close_hartexbeta
- Shuts down the bot.
- :return:
- """
- await super().close()
- def load_commands(self):
- """
- Method: load_commands
- Loads all the commands for the bot.
- :return:
- """
- for filename in os.listdir("./cmds"):
- if filename.endswith(".py"):
- self.load_extension(f"cmds.{filename[:-3]}")
- print(f"HarTex Beta >> Loaded extension {filename[:-3]}.")
- def reload_commands(self):
- """
- Method: reload_commands
- Reloads all the commands for the bot.
- :return:
- """
- for filename in os.listdir("./cmds"):
- if filename.endswith(".py"):
- self.reload_extension(f"cmds.{filename[:-3]}")
- print(f"HarTex Beta >> Reloaded extension {filename[:-3]}.")
- def unload_commands(self):
- """
- Method: unload_commands
- Unloads all the commands for the bot.
- :return:
- """
- for filename in os.listdir("./cmds"):
- if filename.endswith(".py"):
- self.unload_extension(f"cmds.{filename[:-3]}")
- print(f"HarTex Beta >> Unloaded extension {filename[:-3]}.")
- @property
- def uptime(self) -> str:
- """
- Property: uptime
- The bot's uptime.
- :return:
- """
- second = datetime.utcnow() - self.start_time
- minute, second = divmod(second.seconds, 60)
- hour, minute = divmod(minute, 60)
- day, hour = divmod(hour, 24)
- week, day = divmod(day, 7)
- return f"{week} weeks, {day} days, {hour} hours, {minute} minutes, {second} seconds"
- @property
- def token(self) -> str:
- """
- Property: token
- The bot token.
- :return:
- """
- with open("core/token.yaml") as yaml_reader:
- file = yaml.safe_load(yaml_reader)
- return file['token']
- @property
- async def config_connection(self):
- """
- Property: config_connection
- The bot database connection.
- :return:
- """
- return await self.connect_config_database()
- @property
- async def cursor_config(self) -> aiopg.Cursor:
- """
- Property: cursor_config
- The cursor of the existing database connection.
- :return:
- """
- connection = await self.config_connection
- return await connection.cursor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement