This commit is contained in:
2022-07-24 09:53:19 +03:00
parent 5298783ce4
commit 1011e287c9
6 changed files with 36 additions and 16 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@
/user.db
*.json
*.pyc
!/main.py

8
.idea/discord-bot.iml generated
View File

@@ -1,8 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/audio/386629192743256065" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="jdk" jdkName="Python 3.9 (discord-bot)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PackageRequirementsSettings">
<option name="modifyBaseFiles" value="true" />
</component>
</module>

View File

@@ -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

View File

@@ -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

28
lib.py
View File

@@ -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

View File

@@ -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()