From 1011e287c9a23c84ec64cb4522aef8b3fa9e26a9 Mon Sep 17 00:00:00 2001 From: Slava Date: Sun, 24 Jul 2022 09:53:19 +0300 Subject: [PATCH] Moving --- .gitignore | 1 + .idea/discord-bot.iml | 8 +++++++- cogs/general.py | 6 +++--- cogs/info.py | 2 +- lib.py | 32 +++++++++++++++++++++++--------- test.py | 3 +-- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 083a540..45710aa 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /user.db *.json *.pyc +!/main.py diff --git a/.idea/discord-bot.iml b/.idea/discord-bot.iml index a864fcf..792ba6d 100644 --- a/.idea/discord-bot.iml +++ b/.idea/discord-bot.iml @@ -1,8 +1,14 @@ - + + + + + + \ No newline at end of file diff --git a/cogs/general.py b/cogs/general.py index 02c28bd..c5521ae 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -148,11 +148,11 @@ class General(commands.Cog): embed.add_field( name='Prefix', - value= - f'The current prefix for this server is {self.bot.command_prefix}', + value=f'The current prefix for this server is {self.bot.command_prefix}', inline=True) - await inter.response.send_message(embed=embed) + logging.info(f'server prefix {self.bot.command_prefix}') + await inter.response.send_message(embed=embed, ephemeral=True) def setup(bot): # an extension must have a setup function diff --git a/cogs/info.py b/cogs/info.py index 644c15e..d3142de 100644 --- a/cogs/info.py +++ b/cogs/info.py @@ -5,7 +5,7 @@ from disnake.ext import commands class Bot_info(commands.Cog, name='Bot Info'): - def __init__(self, bot): + def __init__(self, bot: object) -> object: self.bot = bot # defining bot as global var in class @commands.Cog.listener() # this is a decorator for events/listeners diff --git a/lib.py b/lib.py index 10d6150..3cca471 100644 --- a/lib.py +++ b/lib.py @@ -21,7 +21,12 @@ class DB: logging.error("failed to connect db", _error) @staticmethod - def work_with_db(db_func, data_turple): + def work_with_db(db_func: str, data_turple: tuple): + """ + Writing to db per server userinfo + :param db_func: + :param data_turple: + """ try: connect = sqlite3.connect('user.db') cursor = connect.cursor() @@ -37,7 +42,7 @@ class DB: sqlite_insert_with_param = (f"""INSERT OR IGNORE INTO "{guild}" (username, userid, nick, isbot) VALUES (?, ?, ?, ?)""") - data_tuple = (name, userid, nick, isbot) + data_tuple: tuple[str, int, str, bool] = (name, userid, nick, isbot) DB.work_with_db(sqlite_insert_with_param, data_tuple) @staticmethod @@ -61,6 +66,10 @@ class DB: class CogsPrepare: + """ + Loads, unloads Cogs files + """ + @staticmethod def cog_list(): cogs_list = [] @@ -94,7 +103,6 @@ class Commands: if not path.isfile('prefix.json'): with open('prefix.json', 'w+', encoding='utf-8') as f: f.write("") - f.close() @staticmethod async def set_prefix(inter, prefix: str): @@ -118,7 +126,7 @@ class Commands: parameter = None try: _json = json.load(fp) # Load the custom prefixes - except: + except TypeError: _json = {} if guild: # If the guild exists try: @@ -129,7 +137,7 @@ class Commands: pass except: pass - fp.close() + return parameter @staticmethod @@ -139,7 +147,6 @@ class Commands: _json = json.load(_f) except json.decoder.JSONDecodeError: _json = {} - _f.close() try: _guild = _json[f'{guild}'] except KeyError: @@ -149,10 +156,15 @@ class Commands: with open('prefix.json', 'w', encoding='utf-8') as _f: json.dump(_json, _f, indent=4) - _f.close() @staticmethod def determine_prefix(bot, msg): + """ + Determite perserver bot prefix + :param bot: + :param msg: + :return: prefix + """ with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON parameter: str try: @@ -160,10 +172,12 @@ class Commands: _json = load(fp) # Load the custom prefixes except: _json = {} + if msg.guild: # If the guild exists try: parameter = _json[f"{msg.guild.id}"]["prefix"] # Read prefix from json if is setted up except: parameter = DEFAULT_PREFIX - print(parameter) - return parameter + + print(parameter) + return parameter diff --git a/test.py b/test.py index 9ca186f..989c12f 100644 --- a/test.py +++ b/test.py @@ -1,13 +1,12 @@ -import json import logging import sys import threading -import lib import disnake from disnake import OptionChoice, OptionType, Option from disnake.ext import commands +import lib bot_owner = 386629192743256065 lib.Commands.check_json()