Advertisement
HTG_YT

classes.py

Apr 10th, 2020
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.26 KB | None | 0 0
  1. class HartexBeta(commands.AutoShardedBot):
  2.     def __init__(self):
  3.         super(HartexBeta, self).__init__(command_prefix="hb.", help_command=None, status=discord.Status.dnd,
  4.                                          activity=discord.Activity(name="in development",
  5.                                                                    type=discord.ActivityType.playing))
  6.         self.start_time = datetime.utcnow()
  7.         self.version = __version_format__(1, 0, 0)
  8.         self.shard_count = 2
  9.  
  10.         self.connection = self.connect_database()
  11.  
  12.     async def on_shard_ready(self, shard_id):
  13.         print(f"HarTex Beta >> Bot shard {shard_id} is ready.")
  14.  
  15.     async def on_command(self, context: commands.Context):
  16.         print(f"HarTex Beta >> Command '{context.command}' is executed by {context.author} in {context.guild}.")
  17.  
  18.     async def on_guild_join(self, guild: discord.Guild):
  19.         print(f"HarTex Beta >> The bot is added to {guild.name}(ID: {guild.id}).")
  20.  
  21.         try:
  22.             self.cursor.execute(sql.SQL("SELECT * FROM guilds"))
  23.  
  24.             guilds = self.cursor.fetchall()
  25.  
  26.             for g in guilds:
  27.                 if guild.id == g[0]:
  28.                     pass
  29.                 else:
  30.                     continue
  31.             else:
  32.                 await guild.leave()
  33.         except psycopg2.ProgrammingError:
  34.             print("HarTex Beta >> Failed to fetch guilds. Please make sure are there any items or data to fetch.")
  35.  
  36.     def run_hartexbeta(self, token: str):
  37.         super().run(token)
  38.  
  39.     def connect_database(self):
  40.         try:
  41.             with open("core/database_credentials.yaml") as yaml_reader:
  42.                 file = yaml.safe_load(yaml_reader)
  43.  
  44.             connection = psycopg2.connect(
  45.                 database=file['database_credentials']['hartex_beta_config']['database'],
  46.                 user=file['database_credentials']['hartex_beta_config']['user'],
  47.                 password=file['database_credentials']['hartex_beta_config']['password'],
  48.                 host=file['database_credentials']['hartex_beta_config']['host'],
  49.                 port=file['database_credentials']['hartex_beta_config']['port']
  50.             )
  51.  
  52.             print(f"HarTex Beta >> Bot is connected to database, connection: {connection}")
  53.  
  54.             return connection
  55.         except psycopg2.OperationalError:
  56.             print("HarTex Beta >> Database connection failure. Please check whether the credentials are correct.")
  57.  
  58.     async def close_hartexbeta(self):
  59.         await super().close()
  60.  
  61.     @property
  62.     def uptime(self) -> str:
  63.         current_time = datetime.utcnow()
  64.  
  65.         difference: timedelta = current_time - self.start_time
  66.  
  67.         # the days and seconds are pretty straight-forward.
  68.         days = difference.days
  69.         seconds = difference.seconds
  70.  
  71.         hours = seconds // 3600  # true division is involved here.
  72.         minutes = (seconds // 60) % 60  # true division and modulus is involved here.
  73.  
  74.         return f"{days}d {hours}h {minutes}m {seconds}s"
  75.  
  76.     @property
  77.     def token(self) -> str:
  78.         with open("core/token.yaml") as yaml_reader:
  79.             file = yaml.safe_load(yaml_reader)
  80.  
  81.             return file['token']
  82.  
  83.     @property
  84.     def cursor(self):
  85.         return self.connection.cursor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement