2020-01-04 02:46:12 +01:00
|
|
|
|
# Created by romain at 04/01/2020
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
import discord
|
|
|
|
|
from discord.ext import commands
|
|
|
|
|
|
|
|
|
|
from bot import TuxBot
|
2020-01-09 13:32:33 +01:00
|
|
|
|
from utils import Texts, GroupPlus
|
2020-01-12 21:43:05 +01:00
|
|
|
|
from utils import FieldPages
|
2020-01-04 02:46:12 +01:00
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HelpCommand(commands.HelpCommand):
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2020-01-09 13:32:33 +01:00
|
|
|
|
self.ignore_cogs = ["Monitoring", "Help", "Jishaku"]
|
|
|
|
|
self.owner_cogs = []
|
2020-01-05 19:49:02 +01:00
|
|
|
|
self.admin_cogs = ["Admin"]
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
2020-01-15 00:08:53 +01:00
|
|
|
|
def command_formatting(self, e, command):
|
|
|
|
|
prefix = self.context.prefix \
|
|
|
|
|
if str(self.context.bot.user.id) not in self.context.prefix \
|
|
|
|
|
else f"@{self.context.bot.user.name}"
|
|
|
|
|
file = Texts(command.cog.qualified_name.lower() + '_help', self.context)
|
|
|
|
|
|
|
|
|
|
if command.parent is not None:
|
|
|
|
|
description = file.get(f"_{command.parent}_{command.name}")
|
|
|
|
|
usage = file.get(f"_{command.parent}_{command.name}__usage")
|
|
|
|
|
else:
|
|
|
|
|
description = file.get(f"_{command.name}")
|
|
|
|
|
usage = file.get(f"_{command.name}__usage")
|
|
|
|
|
|
|
|
|
|
e.title = self.get_command_signature(command) + usage
|
|
|
|
|
e.description = description
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
2020-01-05 19:49:02 +01:00
|
|
|
|
e.add_field(
|
2020-01-09 13:32:33 +01:00
|
|
|
|
name=Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'command_help.params'
|
|
|
|
|
),
|
2020-01-15 00:08:53 +01:00
|
|
|
|
value=usage
|
2020-01-05 19:49:02 +01:00
|
|
|
|
)
|
|
|
|
|
e.add_field(
|
2020-01-09 13:32:33 +01:00
|
|
|
|
name=Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'command_help.usage'
|
|
|
|
|
),
|
2020-01-15 00:08:53 +01:00
|
|
|
|
value=f"{prefix}{command.qualified_name} " + usage
|
2020-01-05 19:49:02 +01:00
|
|
|
|
)
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
|
|
|
|
aliases = "`" + '`, `'.join(command.aliases) + "`"
|
|
|
|
|
if aliases == "``":
|
|
|
|
|
aliases = Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'command_help.no_aliases'
|
|
|
|
|
)
|
2020-01-05 19:49:02 +01:00
|
|
|
|
e.add_field(
|
2020-01-05 01:01:06 +01:00
|
|
|
|
name=Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'command_help.aliases'
|
|
|
|
|
),
|
|
|
|
|
value=aliases
|
|
|
|
|
)
|
|
|
|
|
|
2020-01-05 19:49:02 +01:00
|
|
|
|
return e
|
2020-01-04 02:46:12 +01:00
|
|
|
|
|
|
|
|
|
async def send_bot_help(self, mapping):
|
2020-01-04 21:16:37 +01:00
|
|
|
|
owners = self.context.bot.owners
|
2020-01-15 00:08:53 +01:00
|
|
|
|
prefix = self.context.prefix \
|
|
|
|
|
if str(self.context.bot.user.id) not in self.context.prefix \
|
|
|
|
|
else f"@{self.context.bot.user.name} "
|
2020-01-04 21:16:37 +01:00
|
|
|
|
|
|
|
|
|
e = discord.Embed(
|
2020-01-05 01:01:06 +01:00
|
|
|
|
color=discord.Color.blue(),
|
2020-01-04 21:16:37 +01:00
|
|
|
|
description=Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'main_page.description'
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
e.set_author(
|
|
|
|
|
icon_url=self.context.author.avatar_url_as(format='png'),
|
|
|
|
|
name=self.context.author
|
|
|
|
|
)
|
2020-01-05 01:01:06 +01:00
|
|
|
|
e.set_footer(
|
|
|
|
|
text=Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'main_page.footer'
|
|
|
|
|
).format(
|
|
|
|
|
prefix
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-01-04 02:46:12 +01:00
|
|
|
|
|
|
|
|
|
for extension in self.context.bot.cogs.values():
|
2020-01-04 21:16:37 +01:00
|
|
|
|
if self.context.author not in owners \
|
2020-01-05 01:01:06 +01:00
|
|
|
|
and extension.__class__.__name__ in self.owner_cogs:
|
2020-01-04 02:46:12 +01:00
|
|
|
|
continue
|
2020-01-05 01:01:06 +01:00
|
|
|
|
if extension.__class__.__name__ in self.ignore_cogs:
|
2020-01-04 02:46:12 +01:00
|
|
|
|
continue
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
2020-01-09 13:32:33 +01:00
|
|
|
|
count = len(extension.get_commands())
|
|
|
|
|
text = Texts('help', self.context).get('main_page.commands')
|
2020-01-04 02:46:12 +01:00
|
|
|
|
|
2020-01-09 13:32:33 +01:00
|
|
|
|
if count <= 1:
|
|
|
|
|
text = text[:-1]
|
|
|
|
|
|
|
|
|
|
e.add_field(
|
|
|
|
|
name=f"__{extension.icon} **{extension.qualified_name}**__",
|
|
|
|
|
value=f"{count} {text}"
|
|
|
|
|
)
|
2020-01-04 21:16:37 +01:00
|
|
|
|
|
|
|
|
|
await self.context.send(embed=e)
|
2020-01-04 02:46:12 +01:00
|
|
|
|
|
2020-01-05 01:01:06 +01:00
|
|
|
|
async def send_cog_help(self, cog):
|
|
|
|
|
pages = {}
|
2020-01-15 00:08:53 +01:00
|
|
|
|
prefix = self.context.prefix \
|
|
|
|
|
if str(self.context.bot.user.id) not in self.context.prefix \
|
|
|
|
|
else f"@{self.context.bot.user.name}"
|
|
|
|
|
file = Texts(cog.qualified_name.lower() + '_help', self.context)
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
|
|
|
|
if cog.__class__.__name__ in self.owner_cogs \
|
|
|
|
|
and self.context.author not in self.context.bot.owners:
|
|
|
|
|
return self.command_not_found(cog.qualified_name)
|
|
|
|
|
|
|
|
|
|
for cmd in cog.get_commands():
|
|
|
|
|
if self.context.author not in self.context.bot.owners \
|
|
|
|
|
and (cmd.hidden or cmd.category == "Hidden"):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if cmd.category not in pages:
|
|
|
|
|
pages[cmd.category] = "```asciidoc\n"
|
|
|
|
|
|
|
|
|
|
pages[cmd.category] \
|
|
|
|
|
+= f"{cmd.name}" \
|
2020-01-15 00:08:53 +01:00
|
|
|
|
+ ' ' * int(13 - len(cmd.name)) \
|
|
|
|
|
+ f":: {file.get(f'_{cmd.name}__short')}\n"
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
2020-01-09 13:32:33 +01:00
|
|
|
|
if isinstance(cmd, GroupPlus):
|
2020-01-05 01:01:06 +01:00
|
|
|
|
for group_command in cmd.commands:
|
|
|
|
|
pages[cmd.category] \
|
2020-01-09 13:32:33 +01:00
|
|
|
|
+= f"└> {group_command.name}" \
|
2020-01-15 00:08:53 +01:00
|
|
|
|
+ ' ' * int(10 - len(group_command.name)) \
|
|
|
|
|
+ f":: {file.get(f'_{group_command.parent}_{group_command.name}__short')}\n"
|
2020-01-05 01:01:06 +01:00
|
|
|
|
for e in pages:
|
|
|
|
|
pages[e] += "```"
|
|
|
|
|
formatted = []
|
|
|
|
|
for name, cont in pages.items():
|
|
|
|
|
formatted.append((name, cont))
|
|
|
|
|
footer_text = Texts('help', self.context) \
|
|
|
|
|
.get('main_page.footer') \
|
|
|
|
|
.format(prefix)
|
|
|
|
|
|
|
|
|
|
pages = FieldPages(
|
|
|
|
|
self.context,
|
|
|
|
|
embed_color=discord.Color.blue(),
|
|
|
|
|
entries=formatted,
|
|
|
|
|
title=cog.qualified_name.upper(),
|
|
|
|
|
thumbnail=cog.big_icon,
|
|
|
|
|
footericon=self.context.bot.user.avatar_url,
|
|
|
|
|
footertext=footer_text,
|
|
|
|
|
per_page=1
|
|
|
|
|
)
|
|
|
|
|
await pages.paginate()
|
|
|
|
|
|
|
|
|
|
async def send_group_help(self, group):
|
|
|
|
|
if group.cog_name in self.ignore_cogs:
|
|
|
|
|
return await self.send_error_message(
|
|
|
|
|
self.command_not_found(group.name)
|
|
|
|
|
)
|
2020-01-15 00:08:53 +01:00
|
|
|
|
file = Texts(group.qualified_name.lower() + '_help', self.context)
|
2020-01-05 01:01:06 +01:00
|
|
|
|
|
2020-01-15 00:08:53 +01:00
|
|
|
|
formatted = self.command_formatting(
|
2020-01-05 01:01:06 +01:00
|
|
|
|
discord.Embed(color=discord.Color.blue()),
|
|
|
|
|
group
|
|
|
|
|
)
|
2020-01-15 00:08:53 +01:00
|
|
|
|
sub_command_list = "⠀" # this is braille, please don't touch unless you know what you're doing
|
2020-01-05 01:01:06 +01:00
|
|
|
|
for group_command in group.commands:
|
2020-01-15 00:08:53 +01:00
|
|
|
|
sub_command_list += f"└> **{group_command.name}** - {file.get(f'_{group_command.parent}_{group_command.name}')}\n"
|
|
|
|
|
|
2020-01-05 01:01:06 +01:00
|
|
|
|
subcommands = Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'command_help.subcommands'
|
|
|
|
|
)
|
|
|
|
|
|
2020-01-15 00:08:53 +01:00
|
|
|
|
formatted.add_field(name=subcommands, value=sub_command_list, inline=False)
|
2020-01-05 01:01:06 +01:00
|
|
|
|
await self.context.send(embed=formatted)
|
|
|
|
|
|
|
|
|
|
async def send_command_help(self, command):
|
|
|
|
|
if isinstance(command, commands.Group):
|
|
|
|
|
return await self.send_group_help(command)
|
|
|
|
|
|
|
|
|
|
if command.cog_name in self.ignore_cogs:
|
|
|
|
|
return await self.send_error_message(
|
|
|
|
|
self.command_not_found(command.name))
|
|
|
|
|
|
2020-01-15 00:08:53 +01:00
|
|
|
|
formatted = self.command_formatting(
|
2020-01-05 01:01:06 +01:00
|
|
|
|
discord.Embed(color=discord.Color.blue()),
|
|
|
|
|
command
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await self.context.send(embed=formatted)
|
|
|
|
|
|
|
|
|
|
def command_not_found(self, command):
|
|
|
|
|
return Texts(
|
|
|
|
|
'help', self.context
|
|
|
|
|
).get(
|
|
|
|
|
'main_page.not_found'
|
|
|
|
|
).format(
|
|
|
|
|
command
|
|
|
|
|
)
|
|
|
|
|
|
2020-01-04 02:46:12 +01:00
|
|
|
|
|
|
|
|
|
class Help(commands.Cog):
|
|
|
|
|
def __init__(self, bot: TuxBot):
|
|
|
|
|
bot.help_command = HelpCommand()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot: TuxBot):
|
|
|
|
|
bot.add_cog(Help(bot))
|