Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class HartexBeta(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))
- self.start_time = datetime.utcnow()
- self.version = __version_format__(1, 0, 0)
- self.shard_count = 2
- self.connection = self.connect_database()
- async def on_shard_ready(self, shard_id):
- print(f"HarTex Beta >> Bot shard {shard_id} is ready.")
- async def on_command(self, context: commands.Context):
- print(f"HarTex Beta >> Command '{context.command}' is executed by {context.author} in {context.guild}.")
- async def on_guild_join(self, guild: discord.Guild):
- print(f"HarTex Beta >> The bot is added to {guild.name}(ID: {guild.id}).")
- try:
- self.cursor.execute(sql.SQL("SELECT * FROM guilds"))
- guilds = self.cursor.fetchall()
- for g in guilds:
- if guild.id == g[0]:
- pass
- else:
- continue
- else:
- await guild.leave()
- except psycopg2.ProgrammingError:
- print("HarTex Beta >> Failed to fetch guilds. Please make sure are there any items or data to fetch.")
- def run_hartexbeta(self, token: str):
- super().run(token)
- def connect_database(self):
- try:
- with open("core/database_credentials.yaml") as yaml_reader:
- file = yaml.safe_load(yaml_reader)
- connection = psycopg2.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']
- )
- print(f"HarTex Beta >> Bot is connected to database, connection: {connection}")
- return connection
- except psycopg2.OperationalError:
- print("HarTex Beta >> Database connection failure. Please check whether the credentials are correct.")
- async def close_hartexbeta(self):
- await super().close()
- @property
- def uptime(self) -> str:
- current_time = datetime.utcnow()
- difference: timedelta = current_time - self.start_time
- # the days and seconds are pretty straight-forward.
- days = difference.days
- seconds = difference.seconds
- hours = seconds // 3600 # true division is involved here.
- minutes = (seconds // 60) % 60 # true division and modulus is involved here.
- return f"{days}d {hours}h {minutes}m {seconds}s"
- @property
- def token(self) -> str:
- with open("core/token.yaml") as yaml_reader:
- file = yaml.safe_load(yaml_reader)
- return file['token']
- @property
- def cursor(self):
- return self.connection.cursor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement