Switch utils to slash commands
This commit is contained in:
parent
404cf8b97a
commit
d349a398c5
3 changed files with 46 additions and 45 deletions
|
@ -1,10 +1,12 @@
|
||||||
from discord import Intents
|
from discord import Intents
|
||||||
|
from discord_slash import SlashCommand
|
||||||
|
|
||||||
from administrator.config import config
|
from administrator.config import config
|
||||||
import db
|
import db
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
bot = commands.Bot(command_prefix=config.get("prefix"), intents=Intents.all())
|
bot = commands.Bot(command_prefix=config.get("prefix"), intents=Intents.all())
|
||||||
|
slash = SlashCommand(bot, auto_register=True)
|
||||||
|
|
||||||
import extensions
|
import extensions
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ from discord import Embed
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord.ext.commands import CommandNotFound, MissingRequiredArgument, BadArgument, MissingPermissions, \
|
from discord.ext.commands import CommandNotFound, MissingRequiredArgument, BadArgument, MissingPermissions, \
|
||||||
NoPrivateMessage, CommandError, NotOwner
|
NoPrivateMessage, CommandError, NotOwner
|
||||||
|
from discord_slash import SlashContext
|
||||||
|
|
||||||
from administrator import config
|
from administrator import config
|
||||||
from administrator.check import ExtensionDisabled
|
from administrator.check import ExtensionDisabled
|
||||||
|
@ -38,19 +39,26 @@ class Help(commands.Cog):
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
async def on_command_error(self, ctx: commands.Context, error):
|
async def on_command_error(self, ctx: commands.Context, error):
|
||||||
|
await self.error_handler(ctx, error)
|
||||||
|
|
||||||
|
@commands.Cog.listener()
|
||||||
|
async def on_slash_command_error(self, ctx: SlashContext, error: Exception):
|
||||||
|
await self.error_handler(ctx, error)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def error_handler(ctx, error: Exception):
|
||||||
if isinstance(error, CommandNotFound):
|
if isinstance(error, CommandNotFound):
|
||||||
await ctx.message.add_reaction("\u2753")
|
await ctx.send(content="\u2753")
|
||||||
elif isinstance(error, MissingRequiredArgument) or isinstance(error, BadArgument):
|
elif isinstance(error, MissingRequiredArgument) or isinstance(error, BadArgument):
|
||||||
await ctx.message.add_reaction("\u274C")
|
await ctx.send(content="\u274C")
|
||||||
elif isinstance(error, NotOwner) or isinstance(error, MissingPermissions) \
|
elif isinstance(error, NotOwner) or isinstance(error, MissingPermissions) \
|
||||||
or isinstance(error, NoPrivateMessage):
|
or isinstance(error, NoPrivateMessage):
|
||||||
await ctx.message.add_reaction("\U000026D4")
|
await ctx.send(content="\U000026D4")
|
||||||
elif isinstance(error, ExtensionDisabled):
|
elif isinstance(error, ExtensionDisabled):
|
||||||
await ctx.message.add_reaction("\U0001F6AB")
|
await ctx.send(content="\U0001F6AB")
|
||||||
else:
|
else:
|
||||||
await ctx.send("An error occurred !")
|
await ctx.send(content="An error occurred !")
|
||||||
raise error
|
raise error
|
||||||
await ctx.message.delete(delay=30)
|
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from discord import Embed, Member, Guild
|
from discord import Embed, Guild
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord.ext.commands import BadArgument, CommandError
|
from discord_slash import cog_ext, SlashContext, SlashCommandOptionType
|
||||||
|
from discord_slash.utils import manage_commands
|
||||||
|
|
||||||
|
from administrator import slash
|
||||||
from administrator.check import is_enabled
|
from administrator.check import is_enabled
|
||||||
from administrator.logger import logger
|
from administrator.logger import logger
|
||||||
|
|
||||||
|
@ -15,34 +17,17 @@ logger = logger.getChild(extension_name)
|
||||||
class Utils(commands.Cog):
|
class Utils(commands.Cog):
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
slash.get_cog_commands(self)
|
||||||
|
|
||||||
def description(self):
|
def description(self):
|
||||||
return "Some tools"
|
return "Some tools"
|
||||||
|
|
||||||
@commands.group("utils", pass_context=True)
|
|
||||||
@is_enabled()
|
|
||||||
async def utils(self, ctx: commands.Context):
|
|
||||||
if ctx.invoked_subcommand is None:
|
|
||||||
await ctx.invoke(self.utils_help)
|
|
||||||
|
|
||||||
@utils.group("help", pass_context=True)
|
|
||||||
async def utils_help(self, ctx: commands.Context):
|
|
||||||
embed = Embed(title="Utils help")
|
|
||||||
try:
|
|
||||||
if await self.eval.can_run(ctx):
|
|
||||||
embed.add_field(name="eval \`\`\`code\`\`\`", value="Execute some code", inline=False)
|
|
||||||
except CommandError:
|
|
||||||
pass
|
|
||||||
embed.add_field(name="ping", value="Return the ping with the discord API", inline=False)
|
|
||||||
embed.add_field(name="info [@user]", value="Show information on guild or user specified", inline=False)
|
|
||||||
await ctx.send(embed=embed)
|
|
||||||
|
|
||||||
@commands.group("eval", pass_context=True)
|
@commands.group("eval", pass_context=True)
|
||||||
@commands.is_owner()
|
@commands.is_owner()
|
||||||
async def eval(self, ctx: commands.Context):
|
async def eval(self, ctx: commands.Context):
|
||||||
start = ctx.message.content.find("```")
|
start = ctx.message.content.find("```python")
|
||||||
end = ctx.message.content.find("```", start+3)
|
end = ctx.message.content.find("```", start+9)
|
||||||
command = ctx.message.content[start+3:end]
|
command = ctx.message.content[start+9:end]
|
||||||
try:
|
try:
|
||||||
exec("async def __ex(self, ctx):\n" + command.replace("\n", "\n "))
|
exec("async def __ex(self, ctx):\n" + command.replace("\n", "\n "))
|
||||||
out = str(await locals()["__ex"](self, ctx))
|
out = str(await locals()["__ex"](self, ctx))
|
||||||
|
@ -55,20 +40,20 @@ class Utils(commands.Cog):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await ctx.send(f"```{e.__class__.__name__}: {e}```")
|
await ctx.send(f"```{e.__class__.__name__}: {e}```")
|
||||||
|
|
||||||
@commands.group("ping", pass_context=True)
|
@cog_ext.cog_slash(name="ping", description="Return the ping with the discord API")
|
||||||
@is_enabled()
|
@is_enabled()
|
||||||
async def ping(self, ctx: commands.Context):
|
async def ping(self, ctx: SlashContext):
|
||||||
start = datetime.now()
|
start = datetime.now()
|
||||||
msg = await ctx.send(f"Discord WebSocket latency: `{round(self.bot.latency*1000)}ms`")
|
content = f"Discord WebSocket latency: `{round(self.bot.latency*1000)}ms`"
|
||||||
await msg.edit(content=msg.content+"\n"+f"Bot latency: `{round((msg.created_at - start).microseconds/1000)}ms`")
|
await ctx.send(content=content)
|
||||||
|
await ctx.edit(content=content+"\n"+f"Bot latency: `{round((datetime.now() - start).microseconds/1000)}ms`")
|
||||||
|
|
||||||
@commands.group("info", pass_context=True)
|
@cog_ext.cog_slash(name="info",
|
||||||
|
description="Show information on guild or user specified",
|
||||||
|
options=[manage_commands.create_option("user", "A user", SlashCommandOptionType.USER, False)])
|
||||||
@is_enabled()
|
@is_enabled()
|
||||||
async def info(self, ctx: commands.Context):
|
async def info(self, ctx: SlashContext, user: SlashCommandOptionType.USER = None):
|
||||||
if len(ctx.message.mentions) > 1:
|
if user:
|
||||||
raise BadArgument()
|
|
||||||
elif ctx.message.mentions:
|
|
||||||
user: Member = ctx.message.mentions[0]
|
|
||||||
embed = Embed(title=str(user))
|
embed = Embed(title=str(user))
|
||||||
embed.set_author(name="User infos", icon_url=user.avatar_url)
|
embed.set_author(name="User infos", icon_url=user.avatar_url)
|
||||||
embed.add_field(name="Display name", value=user.display_name)
|
embed.add_field(name="Display name", value=user.display_name)
|
||||||
|
@ -129,11 +114,11 @@ class Utils(commands.Cog):
|
||||||
embed.add_field(name="Shard ID", value=guild.shard_id)
|
embed.add_field(name="Shard ID", value=guild.shard_id)
|
||||||
embed.add_field(name="Created at", value=guild.created_at)
|
embed.add_field(name="Created at", value=guild.created_at)
|
||||||
|
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embeds=[embed])
|
||||||
|
|
||||||
@commands.group("about", pass_context=True)
|
@cog_ext.cog_slash(name="about", description="Show information about the bot")
|
||||||
@is_enabled()
|
@is_enabled()
|
||||||
async def about(self, ctx: commands.Context):
|
async def about(self, ctx: SlashContext):
|
||||||
embed = Embed(title=self.bot.user.display_name, description=self.bot.description)
|
embed = Embed(title=self.bot.user.display_name, description=self.bot.description)
|
||||||
embed.set_author(name="Administrator", icon_url=self.bot.user.avatar_url, url="https://github.com/flifloo")
|
embed.set_author(name="Administrator", icon_url=self.bot.user.avatar_url, url="https://github.com/flifloo")
|
||||||
flifloo = self.bot.get_user(177393521051959306)
|
flifloo = self.bot.get_user(177393521051959306)
|
||||||
|
@ -142,9 +127,12 @@ class Utils(commands.Cog):
|
||||||
value=(await self.bot.application_info()).owner.display_name)
|
value=(await self.bot.application_info()).owner.display_name)
|
||||||
embed.add_field(name="Guilds", value=str(len(self.bot.guilds)))
|
embed.add_field(name="Guilds", value=str(len(self.bot.guilds)))
|
||||||
embed.add_field(name="Extensions", value=str(len(self.bot.extensions)))
|
embed.add_field(name="Extensions", value=str(len(self.bot.extensions)))
|
||||||
embed.add_field(name="Commands", value=str(len(self.bot.all_commands)))
|
embed.add_field(name="Commands", value=str(len(self.bot.all_commands)+len(slash.commands)))
|
||||||
embed.add_field(name="Latency", value=f"{round(self.bot.latency*1000)} ms")
|
embed.add_field(name="Latency", value=f"{round(self.bot.latency*1000)} ms")
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embeds=[embed])
|
||||||
|
|
||||||
|
def cog_unload(self):
|
||||||
|
slash.remove_cog_commands(self)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
|
@ -165,3 +153,6 @@ def teardown(bot):
|
||||||
logger.error(f"Error unloading: {e}")
|
logger.error(f"Error unloading: {e}")
|
||||||
else:
|
else:
|
||||||
logger.info(f"Unload successful")
|
logger.info(f"Unload successful")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue