From d33ff81770032259dcf22413e9bd0e9d10471ef6 Mon Sep 17 00:00:00 2001 From: Slava Date: Tue, 28 Jun 2022 01:37:28 +0300 Subject: [PATCH] testing cog --- .gitignore | 1 + cogs/test.py | 18 +++++++++++++ test.py | 73 +++++++++++++++++++++------------------------------- 3 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 cogs/test.py diff --git a/.gitignore b/.gitignore index 2ac0209..72e128b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ gen /test2.py /user.db /*.json +*.pyc diff --git a/cogs/test.py b/cogs/test.py new file mode 100644 index 0000000..316ea86 --- /dev/null +++ b/cogs/test.py @@ -0,0 +1,18 @@ +from discord.ext import commands + + +class Test_Cog(commands.Cog): + def __init__(self, bot): + self.bot = bot # defining bot as global var in class + + @commands.Cog.listener() # this is a decorator for events/listeners + async def on_ready(self): + print('Bot is ready!.') + + @commands.command() # this is for making a command + async def ping(self, ctx): + await ctx.send(f'Pong! {round(self.bot.latency * 1000)}') + + +def setup(bot): # a extension must have a setup function + bot.add_cog(Test_Cog(bot)) # adding a cog diff --git a/test.py b/test.py index aa331cd..b5eaab8 100644 --- a/test.py +++ b/test.py @@ -3,7 +3,7 @@ import logging import sqlite3 import sys import threading -from os import walk, path, makedirs, remove, rename +from os import walk, path, makedirs, remove, rename, listdir from typing import List, Union, Any import discord @@ -13,7 +13,7 @@ from dislash import InteractionClient, Option, OptionType class DB: @staticmethod - def _prepare_db(guild): + def _prepare_db(guild: int): try: connect = sqlite3.connect('user.db') cursor = connect.cursor() @@ -46,23 +46,19 @@ class DB: DB._work_with_db(sqlite_insert_with_param, data_tuple) @staticmethod - def _add_audio(guildid, user, audio): + def _add_audio(guildid: int, user: int, audio: str): sql_update_query = f"""UPDATE "{guildid}" set track = ? where userid = ?""" data_tuple = (audio, user) DB._work_with_db(sql_update_query, data_tuple) @staticmethod - def _read_db(guildid, user): + def _read_db(guild: int, user: int): try: sql_con = sqlite3.connect("user.db") cursor = sql_con.cursor() - sql_read = f"""SELECT * FROM "{guildid}" where userid = {user}""" + sql_read = f"""SELECT * FROM "{guild}" where userid = {user}""" cursor.execute(sql_read) record = cursor.fetchone() - # if record[4] is None: - # return "None" - # else: - # return record[4] return record[4] except sqlite3.Error as error: @@ -92,47 +88,39 @@ intents.members = True bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents) inter_client = InteractionClient(bot) +for filename in listdir('./cogs'): + if filename.endswith('.py'): + bot.load_extension(f'cogs.{filename[:-3]}') + print(f'Load cog {filename[:-3]}') + else: + print(f'Unable to load {filename[:-3]}') + class Commands: + @staticmethod async def set_prefix(ctx, prefix: str): - with open('prefix.json', 'r', encoding='utf-8') as fp: - try: - jsonObject = json.load(fp) - except: - jsonObject = {} - try: - jsonObject[f"{ctx.guild.id}"]["prefix"] = prefix - except KeyError: - guild = jsonObject[f"{ctx.guild.id}"] = {'prefix': ''} - new = {"prefix": prefix} - guild.update(new) - jsonObject[str(ctx.guild.id)] = guild - + await Commands._write_json(ctx.guild.id, "prefix", prefix) await ctx.send(f"Prefix set to: `{prefix}`") - with open("prefix.json", "w") as fl: - json.dump(jsonObject, fl) - fl.close() - - def list_files(folder): - f = [] - for filenames in walk(folder): - f.extend(filenames) + @staticmethod + def list_files(_fold: str): + fl = [] + for filenames in walk(_fold): + fl.extend(filenames) break - f = f[2] files = {} - for x in f: + for x in fl[2]: files[x] = x return files - async def _read_json(guild, param): + @staticmethod + async def _read_json(guild: int, param): parameter = None try: with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON jsonObject = json.load(fp) # Load the custom prefixes - - except: + except TypeError: jsonObject = {} if guild: # If the guild exists @@ -140,30 +128,29 @@ class Commands: guild_conf = jsonObject[f"{guild}"] try: parameter = guild_conf[f"{param}"] - except: + except TypeError: pass except KeyError: pass - + fp.close() return parameter + @staticmethod async def _write_json(guild, param_name, param): with open('prefix.json', 'r', encoding='utf-8') as fp: try: jsonObject = json.load(fp) - except: + except TypeError: jsonObject = {} try: jsonObject[f"{guild}"][f"{param_name}"] = param except KeyError: json_param = jsonObject[f"{guild}"] = {f'{param_name}': ''} - new = {f"{param_name}": param} - json_param.update(new) - jsonObject[str(guild)] = json_param + jsonObject[str(guild)] = json_param.update({f"{param_name}": param}) with open("prefix.json", "w") as fl: json.dump(jsonObject, fl) - fl.close() + fl.close() @bot.event @@ -253,7 +240,7 @@ async def upload_audio(ctx, user=None): @bot.command(name="set_prefix") @commands.has_permissions(administrator=True) async def command_setprefix(ctx, prefix: str): - await Commands.set_prefix(ctx, prefix) + await self.Commands.set_prefix(ctx, prefix) @inter_client.slash_command(