From 3456faed4ec1efaf08adbbf28bbfcf23120ad47c Mon Sep 17 00:00:00 2001 From: Slava Date: Fri, 1 Jul 2022 00:23:20 +0300 Subject: [PATCH] Fixed read prefix First iteration work with cog from slash command --- cogs/{test.py => first_cog.py} | 4 +- test.py | 93 +++++++++++++++++----------------- 2 files changed, 49 insertions(+), 48 deletions(-) rename cogs/{test.py => first_cog.py} (86%) diff --git a/cogs/test.py b/cogs/first_cog.py similarity index 86% rename from cogs/test.py rename to cogs/first_cog.py index 316ea86..d7cb195 100644 --- a/cogs/test.py +++ b/cogs/first_cog.py @@ -1,3 +1,5 @@ +import logging + from discord.ext import commands @@ -7,7 +9,7 @@ class Test_Cog(commands.Cog): @commands.Cog.listener() # this is a decorator for events/listeners 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 async def ping(self, ctx): diff --git a/test.py b/test.py index a64ca14..df40253 100644 --- a/test.py +++ b/test.py @@ -10,16 +10,26 @@ import dislash from discord.ext import commands 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): guild = msg.guild - base = Commands.read_json(guild.id, 'prefix') or DEFAULT_PREFIX # Get bot role - return base + with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON + 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.members = True bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents) @@ -89,18 +99,6 @@ class CogsPrepare: cogs_list.append(_filename[:-3]) 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 async def work_with_cogs(what_do, cogs): for _filename in cogs: @@ -116,16 +114,16 @@ class CogsPrepare: async def cog_load(ctx, cog): try: 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: - 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: - 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: - await ctx.send(f'Error: unknown error with {cog}') + await ctx.send(f'Error: unknown error with {cog}', ephemeral=True) class Commands: @@ -148,20 +146,20 @@ class Commands: @staticmethod async def read_json(guild: int, param: str): - parameter = None - try: - with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON + with open('prefix.json', 'r', encoding='utf-8') as fp: # Open the JSON + parameter = None + try: _json = json.load(fp) # Load the custom prefixes - except TypeError: - _json = {} + except: + _json = {} if guild: # If the guild exists try: guild_conf = _json[f"{guild}"] try: parameter = guild_conf[f"{param}"] - except TypeError: + except: pass - except KeyError: + except: pass fp.close() return parameter @@ -275,7 +273,7 @@ async def upload_audio(ctx, user=None): file = f'tmp/{user.id}/{at.filename}' duration = round(MediaInfo.parse(file).tracks[0].duration / 1000) 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: a = DB.read_db(ctx.guild.id, user.id) if a is None: @@ -286,12 +284,12 @@ async def upload_audio(ctx, user=None): DB.add_audio(ctx.guild.id, user.id, audiolist) rename(f'tmp/{user.id}/{at.filename}', f'audio/{user.id}/{at.filename}') 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}') else: - await ctx.reply("Has no Attachment") + await ctx.reply("Has no Attachment", ephemeral=True) 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") @@ -315,7 +313,7 @@ async def slash_set_prefix(ctx, prefix: str): @slash_set_prefix.error @command_set_prefix.error 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( @@ -386,30 +384,31 @@ async def set_bot_role(ctx, role): name="cog", description="Work with cogs", options=[ - Option("what do", - description="Specify what do with cogs", - type=OptionType.STRING, - required=True, - choices=[ - OptionChoice("load", "load"), - OptionChoice("unload", "unload"), - OptionChoice("reload", "reload"), - ] - ), + Option( + "what_do", + description="Specify what do with cogs", + type=OptionType.STRING, + required=True, + choices=[ + OptionChoice("load", "load"), + OptionChoice("unload", "unload"), + OptionChoice("reload", "reload") + ] + ), 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: cogs = CogsPrepare.cog_list() if ctx.author.id == bot_owner: await CogsPrepare.work_with_cogs(what_do, cogs) 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: - await ctx.reply(f'Cog {cogs} is {what_do}ed') + await ctx.reply(f'Cog {cogs[0]} is {what_do}ed', ephemeral=True) else: - await ctx.reply('You`re not bot owner') + await ctx.reply('You`re not bot owner', ephemeral=True) @inter_client.slash_command(