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

93
test.py
View File

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