1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-03 00:23:13 +00:00
Lumi/services/Help.py

117 lines
4.4 KiB
Python
Raw Normal View History

2024-03-16 20:08:48 +00:00
import discord
from discord.ext import commands
2024-03-18 14:40:58 +00:00
from lib.embeds.error import HelpErrors
from dotenv import load_dotenv
2024-03-25 20:18:33 +00:00
from lib import checks
import os
2024-03-16 20:08:48 +00:00
load_dotenv('.env')
2024-03-16 20:08:48 +00:00
2024-03-23 10:45:48 +00:00
2024-03-17 12:59:13 +00:00
class RacuHelp(commands.HelpCommand):
def __init__(self, **options):
super().__init__(**options)
2024-03-25 20:18:33 +00:00
self.verify_checks = True
self.command_attrs = {
"aliases": ["h"],
"help": "Show a list of commands, or information about a specific command when an argument is passed.",
"name": "help"
}
2024-03-17 12:59:13 +00:00
def get_command_qualified_name(self, command):
return '`%s%s`' % (self.context.clean_prefix, command.qualified_name)
2024-03-17 12:59:13 +00:00
async def send_bot_help(self, mapping):
embed = discord.Embed(color=discord.Color.blurple())
2024-03-18 14:40:58 +00:00
2024-03-25 20:18:33 +00:00
for cog, racu_commands in mapping.items():
2024-03-25 20:18:33 +00:00
filtered = await self.filter_commands(racu_commands, sort=True)
2024-03-17 12:59:13 +00:00
if command_signatures := [
self.get_command_qualified_name(c) for c in filtered
2024-03-17 12:59:13 +00:00
]:
# Remove duplicates using set() and convert back to a list
unique_command_signatures = list(set(command_signatures))
cog_name = getattr(cog, "qualified_name", "Help")
embed.add_field(name=cog_name, value=", ".join(sorted(unique_command_signatures)), inline=False)
2024-03-17 12:59:13 +00:00
channel = self.get_destination()
await channel.send(embed=embed)
async def send_command_help(self, command):
2024-03-29 11:14:31 +00:00
embed = discord.Embed(
title=f"{self.context.clean_prefix}{command.qualified_name}",
color=discord.Color.blurple(),
description=command.help
)
2024-03-17 12:59:13 +00:00
usage_value = '`%s%s %s`' % (self.context.clean_prefix, command.qualified_name, command.signature)
for alias in command.aliases:
usage_value += '\n`%s%s %s`' % (self.context.clean_prefix, alias, command.signature)
2024-03-29 11:14:31 +00:00
embed.add_field(name="Usage", value=usage_value, inline=False)
2024-03-17 12:59:13 +00:00
channel = self.get_destination()
await channel.send(embed=embed)
2024-03-18 14:40:58 +00:00
async def send_error_message(self, error):
channel = self.get_destination()
await channel.send(embed=HelpErrors.error_message(self.context, error))
2024-03-17 12:59:13 +00:00
async def send_group_help(self, group):
2024-03-18 14:40:58 +00:00
channel = self.get_destination()
await channel.send(embed=HelpErrors.error_message(self.context,
f"No command called \"{group.qualified_name}\" found."))
2024-03-17 12:59:13 +00:00
async def send_cog_help(self, cog):
2024-03-18 14:40:58 +00:00
channel = self.get_destination()
await channel.send(embed=HelpErrors.error_message(self.context,
f"No command called \"{cog.qualified_name}\" found."))
2024-03-25 20:18:33 +00:00
async def command_callback(self, ctx, *, command=None):
await self.prepare_help_command(ctx, command)
bot = ctx.bot
if command is None:
mapping = self.get_bot_mapping()
return await self.send_bot_help(mapping)
# Check if it's a cog
cog = bot.get_cog(command)
if cog is not None:
return await self.send_cog_help(cog)
maybe_coro = discord.utils.maybe_coroutine
# If it's not a cog then it's a command.
# Since we want to have detailed errors when someone
# passes an invalid subcommand, we need to walk through
# the command group chain ourselves.
keys = command.split(" ")
cmd = bot.all_commands.get(keys[0].removeprefix(self.context.prefix))
if cmd is None:
string = await maybe_coro(
self.command_not_found, self.remove_mentions(keys[0])
)
return await self.send_error_message(string)
for key in keys[1:]:
try:
found = cmd.all_commands.get(key)
except AttributeError:
string = await maybe_coro(
self.subcommand_not_found, cmd, self.remove_mentions(key)
)
return await self.send_error_message(string)
else:
if found is None:
string = await maybe_coro(
self.subcommand_not_found, cmd, self.remove_mentions(key)
)
return await self.send_error_message(string)
cmd = found
return await self.send_command_help(cmd)