Fixed read prefix

First iteration work with cog from slash command
This commit is contained in:
2022-07-01 00:23:20 +03:00
parent 6ca1e372fc
commit 3456faed4e
2 changed files with 49 additions and 48 deletions

View File

@@ -1,3 +1,5 @@
import logging
from discord.ext import commands from discord.ext import commands
@@ -7,7 +9,7 @@ class Test_Cog(commands.Cog):
@commands.Cog.listener() # this is a decorator for events/listeners @commands.Cog.listener() # this is a decorator for events/listeners
async def on_ready(self): async def on_ready(self):
print('Bot is ready!.') logging.info(f'Cog {__name__.split(".")[1]} is ready!.')
@commands.command() # this is for making a command @commands.command() # this is for making a command
async def ping(self, ctx): async def ping(self, ctx):

93
test.py
View File

@@ -10,16 +10,26 @@ import dislash
from discord.ext import commands from discord.ext import commands
from dislash import InteractionClient, Option, OptionType, OptionChoice from dislash import InteractionClient, Option, OptionType, OptionChoice
DEFAULT_PREFIX = "$" # The prefix you want everyone to have if you don't define your own
bot_owner = 386629192743256065
def determine_prefix(bot, msg): def determine_prefix(bot, msg):
guild = msg.guild guild = msg.guild
base = Commands.read_json(guild.id, 'prefix') or DEFAULT_PREFIX # Get bot role with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
return base parameter = DEFAULT_PREFIX
try:
_json = json.load(fp) # Load the custom prefixes
except:
_json = {}
if guild: # If the guild exists
try:
parameter = _json[f"{guild.id}"]["prefix"]
except:
pass
return parameter
bot_owner = 386629192743256065
DEFAULT_PREFIX = "$" # The prefix you want everyone to have if you don't define your own
intents = discord.Intents.default() intents = discord.Intents.default()
intents.members = True intents.members = True
bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents) bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents)
@@ -89,18 +99,6 @@ class CogsPrepare:
cogs_list.append(_filename[:-3]) cogs_list.append(_filename[:-3])
return cogs_list return cogs_list
@staticmethod
async def cogs_load():
for _filename in CogsPrepare.cog_list():
bot.load_extension(f'cogs.{_filename}')
print(f'Load cog {_filename}')
@staticmethod
async def cogs_unload():
for _filename in CogsPrepare.cog_list():
bot.unload_extension(f'cogs.{_filename}')
print(f'Unload cog {_filename}')
@staticmethod @staticmethod
async def work_with_cogs(what_do, cogs): async def work_with_cogs(what_do, cogs):
for _filename in cogs: for _filename in cogs:
@@ -116,16 +114,16 @@ class CogsPrepare:
async def cog_load(ctx, cog): async def cog_load(ctx, cog):
try: try:
bot.load_extension(f'cogs.{cog}') bot.load_extension(f'cogs.{cog}')
await ctx.reply(f'Loaded cog {cog}') await ctx.reply(f'Loaded cog {cog}', ephemeral=True)
except commands.ExtensionNotFound: except commands.ExtensionNotFound:
await ctx.send(f"Error: {cog} couldn't be found to load.") await ctx.send(f"Error: {cog} couldn't be found to load.", ephemeral=True)
except commands.ExtensionFailed: except commands.ExtensionFailed:
await ctx.send(f'Error: {cog} failed to load properly.') await ctx.send(f'Error: {cog} failed to load properly.', ephemeral=True)
except commands.ExtensionError: except commands.ExtensionError:
await ctx.send(f'Error: unknown error with {cog}') await ctx.send(f'Error: unknown error with {cog}', ephemeral=True)
class Commands: class Commands:
@@ -148,20 +146,20 @@ class Commands:
@staticmethod @staticmethod
async def read_json(guild: int, param: str): async def read_json(guild: int, param: str):
parameter = None with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON
try: parameter = None
with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON try:
_json = json.load(fp) # Load the custom prefixes _json = json.load(fp) # Load the custom prefixes
except TypeError: except:
_json = {} _json = {}
if guild: # If the guild exists if guild: # If the guild exists
try: try:
guild_conf = _json[f"{guild}"] guild_conf = _json[f"{guild}"]
try: try:
parameter = guild_conf[f"{param}"] parameter = guild_conf[f"{param}"]
except TypeError: except:
pass pass
except KeyError: except:
pass pass
fp.close() fp.close()
return parameter return parameter
@@ -275,7 +273,7 @@ async def upload_audio(ctx, user=None):
file = f'tmp/{user.id}/{at.filename}' file = f'tmp/{user.id}/{at.filename}'
duration = round(MediaInfo.parse(file).tracks[0].duration / 1000) duration = round(MediaInfo.parse(file).tracks[0].duration / 1000)
if duration > 15: if duration > 15:
await ctx.reply(f'Audio duration is {duration}, but max is 15') await ctx.reply(f'Audio duration is {duration}, but max is 15', ephemeral=True)
else: else:
a = DB.read_db(ctx.guild.id, user.id) a = DB.read_db(ctx.guild.id, user.id)
if a is None: if a is None:
@@ -286,12 +284,12 @@ async def upload_audio(ctx, user=None):
DB.add_audio(ctx.guild.id, user.id, audiolist) DB.add_audio(ctx.guild.id, user.id, audiolist)
rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}') rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}')
else: else:
await ctx.reply(f'Failed to find MIME type of {at.filename}') await ctx.reply(f'Failed to find MIME type of {at.filename}', ephemeral=True)
remove(f'tmp/{user.id}/{at.filename}') remove(f'tmp/{user.id}/{at.filename}')
else: else:
await ctx.reply("Has no Attachment") await ctx.reply("Has no Attachment", ephemeral=True)
else: else:
await ctx.reply(f'You`re not admin. You can add audio only for your own account') await ctx.reply(f'You`re not admin. You can add audio only for your own account', ephemeral=True)
@bot.command(name="set_prefix") @bot.command(name="set_prefix")
@@ -315,7 +313,7 @@ async def slash_set_prefix(ctx, prefix: str):
@slash_set_prefix.error @slash_set_prefix.error
@command_set_prefix.error @command_set_prefix.error
async def set_prefix_error(ctx): async def set_prefix_error(ctx):
await ctx.reply('You don`t have permissions') await ctx.reply('You don`t have permissions', ephemeral=True)
@inter_client.slash_command( @inter_client.slash_command(
@@ -386,30 +384,31 @@ async def set_bot_role(ctx, role):
name="cog", name="cog",
description="Work with cogs", description="Work with cogs",
options=[ options=[
Option("what do", Option(
description="Specify what do with cogs", "what_do",
type=OptionType.STRING, description="Specify what do with cogs",
required=True, type=OptionType.STRING,
choices=[ required=True,
OptionChoice("load", "load"), choices=[
OptionChoice("unload", "unload"), OptionChoice("load", "load"),
OptionChoice("reload", "reload"), OptionChoice("unload", "unload"),
] OptionChoice("reload", "reload")
), ]
),
Option('cog', "Specify cog if need", OptionType.STRING) Option('cog', "Specify cog if need", OptionType.STRING)
] ]
) )
async def slash_cogs(ctx, what_do, cogs): async def slash_cogs(ctx, what_do, cogs=None):
if cogs is None: if cogs is None:
cogs = CogsPrepare.cog_list() cogs = CogsPrepare.cog_list()
if ctx.author.id == bot_owner: if ctx.author.id == bot_owner:
await CogsPrepare.work_with_cogs(what_do, cogs) await CogsPrepare.work_with_cogs(what_do, cogs)
if cogs is CogsPrepare.cog_list(): if cogs is CogsPrepare.cog_list():
await ctx.reply(f'Cogs are f{what_do}ed') await ctx.reply(f'Cogs are {what_do}ed', ephemeral=True)
else: else:
await ctx.reply(f'Cog {cogs} is {what_do}ed') await ctx.reply(f'Cog {cogs[0]} is {what_do}ed', ephemeral=True)
else: else:
await ctx.reply('You`re not bot owner') await ctx.reply('You`re not bot owner', ephemeral=True)
@inter_client.slash_command( @inter_client.slash_command(