dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
680 stars 161 forks source link

Help command not working correctly #464

Closed vined-underscore closed 1 year ago

vined-underscore commented 1 year ago

Summary

When using a custom help command in discord.py-self 1.9.2, it used to work normally, but after updating to 2.0.0a2, it stopped working and using an on_command event it just spams the command in the logging. (After a while it spams the help command on Discord too)

Reproduction Steps

Overriding the default help command and loading it as a cog.

Code

on_command event:

@vbot.Cog.listener()
async def on_command(self, ctx):
  if not config.logging: return
  msg = ctx.message

  if msg.author == self.bot.user:
    if msg.content.startswith(f"{main.prefix}help"):
      await msg.edit(delete_after = 15.5)

  now = datetime.now()
  time = now.strftime("%H:%M:%S %d-%m-%Y")
  print(f"{F.LIGHTBLACK_EX}[{time}] {F.GREEN}Sent command {F.WHITE}| {F.LIGHTBLUE_EX}{msg.content} {F.LIGHTWHITE_EX}at {f'#{ctx.channel.name}' if not isinstance(ctx.channel, discord.DMChannel) else ctx.channel}")

Help command:

import main
import selfcord as discord
from selfcord.ext import commands as vbot

invisible_cogs = ['nitro sniper', 'events', 'errorhandler']

class HelpCommand(
    vbot.HelpCommand):
    async def send_bot_help(self, mapping):
        desc = []
        for cog, cmds in mapping.items():
            if cog is None or cog.qualified_name.lower() in invisible_cogs:
                continue

            cmds = await self.filter_commands(cmds, sort=True)

            desc.append(f"- {cog.qualified_name}: {len(cmds)} commands")

        desc = "\n".join(desc)

        msg = f"""```yaml
> Made by {self.context.bot.get_user(main.__author_id__)} | {main.__author_id__} <

- {len([command for command in self.context.bot.walk_commands()])} Commands (and subcommands) -  VBot v{main.__version__}

< ---|-

{desc}

-|--- >

- Run {self.context.clean_prefix}{self.invoked_with} [category] to learn more about a category and its commands
- The category name must be case-sensitive. Example: "{self.context.clean_prefix}help Fun"```"""
        await self.get_destination().send(msg, delete_after = 15)

    async def send_group_help(self, group):
        desc = ""
        cmd_desc = \
        f"Name: {group.name}\nCategory: {group.cog_name}\nInfo: {group.help or group.short_doc or group.description}\n\n" \
        f"Aliases:\n - {', '.join(group.aliases) if group.aliases else 'None'}\n\n" \
        f"Usage: {group.name} {group.signature}"
        cmds = await self.filter_commands(group.commands, sort=True)
        for c in cmds:
            desc += f"  - {self.context.clean_prefix}{c.qualified_name}: {c.description or 'no help Information'}\n"

        msg = f"""```yaml
> Made by {self.context.bot.get_user(main.__author_id__)} | {main.__author_id__} <

Group Info: {group.qualified_name}

< ---|-

{cmd_desc}

Group Commands:
{desc}

-|--- >

- [optional], <required>, "=" indicates the default value
- Run {self.context.clean_prefix}{self.invoked_with} {group.name} [subcommand] to learn more about a subcommand```"""
        await self.get_destination().send(msg, delete_after = 15)

    async def send_command_help(self, cmd):
        desc = \
        f"Name: {cmd.name}\nCategory: {cmd.cog_name}\nInfo: {cmd.help or cmd.short_doc or cmd.description}\n\n" \
        f"Aliases:\n - {', '.join(cmd.aliases) if cmd.aliases else 'None'}\n\n" \
        f"Usage: {cmd.name} {cmd.signature}"

        msg = f"""```yaml
> Made by {self.context.bot.get_user(main.__author_id__)} | {main.__author_id__} <

Command Info: {cmd.qualified_name}

< ---|-

{desc}

-|--- >

- [optional], <required>, "=" indicates the default value```"""
        await self.get_destination().send(msg, delete_after = 15)

    async def send_cog_help(self, cog):
        cmds = await self.filter_commands(cog.get_commands(), sort=True)
        cmds = "\n".join(f"- {self.context.clean_prefix}{cmd.name}: {cmd.description or cmd.short_doc}" for cmd in cmds)
        msg = f"""```yaml
> Made by {self.context.bot.get_user(main.__author_id__)} | {main.__author_id__} <

Category Info: {cog.qualified_name}

< ---|-

{cmds}

-|--- >

- Run {self.context.clean_prefix}{self.invoked_with} [command] to learn more about a command```"""
        await self.get_destination().send(msg, delete_after = 15)

    def command_not_found(self, string: str, /) -> str:
        return f"```yaml\n- No command called \"{string}\" found.\n- Use \"{self.context.clean_prefix}help\" to see a list of all the commands.```"

    def subcommand_not_found(self, command, string: str, /) -> str:
        if isinstance(command, discord.Group) and len(command.all_commands) > 0:
            return f"```yaml\n- Command \"{command.qualified_name}\" has no subcommand named {string}.\n- Do \"{self.context.clean_prefix}help [{command.qualified_name}] to see a list of all the subcommands.```"

        return f"```yaml\n- Command \"{command.qualified_name}\" has no subcommands.```"

async def setup(client):
    client._default_help_command = client.help_command
    client.help_command = HelpCommand()

def teardown(client):
    client.help_command = client._default_help_command

Expected Results

Help command sends once.

Actual Results

The help command gets spammed in the logging and after a few minutes it gets spammed in the chat too.

System Information

Checklist

Additional Information

There is no traceback. I am using selfcord but it also did the same thing with discord.py-self 2.0.0a2. image image

vined-underscore commented 1 year ago

Sending the help command in a DM doesn't spam it instantly but sending it in a channel does.

vined-underscore commented 1 year ago

For some reason removing this:

  if msg.author == self.bot.user:
    if msg.content.startswith(f"{main.prefix}help"):
      await msg.edit(delete_after = 15.5)

in the on_command event fixed it.