|
| 1 | +from typing import Union, Optional, Any |
| 2 | + |
| 3 | +import discord |
| 4 | +from discord.ext import commands |
| 5 | + |
| 6 | +from core import checks |
| 7 | +from cogs.utility import PermissionLevel, UnseenFormatter |
| 8 | + |
| 9 | +from bot import ModmailBot |
| 10 | + |
| 11 | + |
| 12 | +class Rhelp(commands.Cog): |
| 13 | + def __init__(self, bot: ModmailBot): |
| 14 | + self.bot = bot |
| 15 | + |
| 16 | + async def get_help_embed( |
| 17 | + self, command: Union[commands.Command, commands.Group], ctx |
| 18 | + ) -> Optional[discord.Embed]: |
| 19 | + """ |
| 20 | + Gets the help embed from the ModMailHelp Command. |
| 21 | +
|
| 22 | + """ |
| 23 | + help_command = self.bot.help_command |
| 24 | + |
| 25 | + help_command.context = ctx |
| 26 | + help_command.context.bot = self.bot |
| 27 | + |
| 28 | + result = await help_command._get_help_embed(command) |
| 29 | + |
| 30 | + if not result: |
| 31 | + return None |
| 32 | + |
| 33 | + embed, perm_level = result |
| 34 | + |
| 35 | + if isinstance(command, commands.Group): |
| 36 | + embed.add_field(name="Permission Level", value=perm_level, inline=False) |
| 37 | + elif isinstance(command, commands.Command): |
| 38 | + embed.set_footer(text=f"Permission level: {perm_level}") |
| 39 | + |
| 40 | + return embed |
| 41 | + |
| 42 | + async def get_config_embed(self, command) -> Optional[discord.Embed]: |
| 43 | + splitted_cmd = command.split("config_")[1] |
| 44 | + config_help = self.bot.config.config_help |
| 45 | + embed = None |
| 46 | + |
| 47 | + def fmt(val): |
| 48 | + return UnseenFormatter().format(val, prefix=self.bot.prefix, bot=self.bot) |
| 49 | + |
| 50 | + if ( |
| 51 | + not ( |
| 52 | + splitted_cmd in self.bot.config.public_keys or splitted_cmd in self.bot.config.protected_keys |
| 53 | + ) |
| 54 | + or splitted_cmd not in config_help |
| 55 | + ): |
| 56 | + return None |
| 57 | + |
| 58 | + for i, (current_key, info) in enumerate(config_help.items()): |
| 59 | + if current_key == splitted_cmd: |
| 60 | + config_embed = discord.Embed(title=f"{current_key}", color=self.bot.main_color) |
| 61 | + config_embed.add_field(name="Default:", value=fmt(info["default"]), inline=False) |
| 62 | + config_embed.add_field(name="Information:", value=fmt(info["description"]), inline=False) |
| 63 | + if info["examples"]: |
| 64 | + example_text = "" |
| 65 | + for example in info["examples"]: |
| 66 | + example_text += f"- {fmt(example)}\n" |
| 67 | + config_embed.add_field(name="Example(s):", value=example_text, inline=False) |
| 68 | + |
| 69 | + note_text = "" |
| 70 | + for note in info.get("notes", []): |
| 71 | + note_text += f"- {fmt(note)}\n" |
| 72 | + if note_text: |
| 73 | + config_embed.add_field(name="Note(s):", value=note_text, inline=False) |
| 74 | + |
| 75 | + if info.get("image") is not None: |
| 76 | + config_embed.set_image(url=fmt(info["image"])) |
| 77 | + |
| 78 | + if info.get("thumbnail") is not None: |
| 79 | + config_embed.set_thumbnail(url=fmt(info["thumbnail"])) |
| 80 | + |
| 81 | + embed = config_embed |
| 82 | + break |
| 83 | + return embed |
| 84 | + |
| 85 | + @commands.command(name="rhelp") |
| 86 | + @checks.thread_only() |
| 87 | + @checks.has_permissions(PermissionLevel.SUPPORTER) |
| 88 | + async def rhelp(self, ctx: commands.Context, *, command: str): |
| 89 | + """ |
| 90 | + Sendet die Hilfe für einen bestimmten Befehl an den Nutzer im aktuellen Thread. |
| 91 | + """ |
| 92 | + command = command.lower() |
| 93 | + embed = None |
| 94 | + if not command.startswith("config_"): |
| 95 | + bot_command = self.bot.get_command(command) |
| 96 | + if not bot_command: |
| 97 | + return await ctx.send(f"Bot Command `{command}` not found.") |
| 98 | + help_embed = await self.get_help_embed(bot_command, ctx) |
| 99 | + if help_embed is None: |
| 100 | + return await ctx.send( |
| 101 | + f"Something went wrong while generating the help embed for the command `{command}`." |
| 102 | + ) |
| 103 | + help_embed.set_author(name=ctx.author, icon_url=ctx.author.display_avatar.url) |
| 104 | + embed = help_embed |
| 105 | + else: |
| 106 | + |
| 107 | + config_embed = await self.get_config_embed(command) |
| 108 | + if not config_embed: |
| 109 | + return await ctx.send(f"Configuration key `{command.split('_config')[1]}` not found.") |
| 110 | + config_embed.set_author(name=ctx.author, icon_url=ctx.author.display_avatar.url) |
| 111 | + embed = config_embed |
| 112 | + |
| 113 | + if embed: |
| 114 | + try: |
| 115 | + await ctx.thread.recipient.send(embed=embed) |
| 116 | + await ctx.send(f"Command help for `{command}` sent to the user.") |
| 117 | + except Exception: |
| 118 | + await ctx.send(f"The command help could not get sent to the user.") |
| 119 | + |
| 120 | + @commands.command(name="arhelp") |
| 121 | + @checks.thread_only() |
| 122 | + @checks.has_permissions(PermissionLevel.SUPPORTER) |
| 123 | + async def arhelp(self, ctx: commands.Context, *, command: str): |
| 124 | + """ |
| 125 | + Sendet die Hilfe für einen bestimmten Befehl an den Nutzer im aktuellen Thread. |
| 126 | + """ |
| 127 | + command = command.lower() |
| 128 | + embed = None |
| 129 | + if not command.startswith("config_"): |
| 130 | + bot_command = self.bot.get_command(command) |
| 131 | + if not bot_command: |
| 132 | + return await ctx.send(f"Bot Command `{command}` not found.") |
| 133 | + help_embed = await self.get_help_embed(bot_command, ctx) |
| 134 | + if help_embed is None: |
| 135 | + return await ctx.send( |
| 136 | + f"Something went wrong while generating the help embed for the command `{command}`." |
| 137 | + ) |
| 138 | + help_embed.set_author( |
| 139 | + name="Modmail Support Agent", |
| 140 | + icon_url="https://discordapp.com/assets/f78426a064bc9dd24847519259bc42af.png", |
| 141 | + ) |
| 142 | + embed = help_embed |
| 143 | + else: |
| 144 | + |
| 145 | + config_embed = await self.get_config_embed(command) |
| 146 | + if not config_embed: |
| 147 | + return await ctx.send(f"Configuration key `{command.split('_config')[1]}` not found.") |
| 148 | + config_embed.set_author( |
| 149 | + name="Modmail Support Agent", |
| 150 | + icon_url="https://discordapp.com/assets/f78426a064bc9dd24847519259bc42af.png", |
| 151 | + ) |
| 152 | + embed = config_embed |
| 153 | + |
| 154 | + if embed: |
| 155 | + try: |
| 156 | + await ctx.thread.recipient.send(embed=embed) |
| 157 | + await ctx.send(f"Command help for `{command}` sent to the user.") |
| 158 | + except Exception: |
| 159 | + await ctx.send(f"The command help could not get sent to the user.") |
| 160 | + |
| 161 | + |
| 162 | +async def setup(bot: commands.Bot): |
| 163 | + await bot.add_cog(Rhelp(bot)) |
0 commit comments