Skip to content

[BUG] HybridContext Initialization from DM #1490

Closed
@itchono

Description

@itchono

Library Version

5.8.0

Describe the Bug

When calling a hybrid slash command from a DM channel as a prefixed command, an error occurs while trying to create a HybridContext from a PrefixedContext, due to ctx.channel being None.

Steps to Reproduce

  1. Create a hybrid slash command, with dm_permission = True (True by default)
  2. In a DM with the bot, invoke the command using the prefixed commands route (i.e. NOT using slash commands)

(Example, with traceback printout omitted)
image

Expected Results

The command should invoke normally.

Minimal Reproducible Code

from interactions import Client
from interactions.ext import hybrid_commands, prefixed_commands

client = Client()

prefixed_commands.setup(client)
hybrid_commands.setup(client)


@hybrid_commands.hybrid_slash_command(name="cmd1", description="broken in DMs")
async def cmd1(ctx: hybrid_commands.HybridContext):
    # works fine in a guild, but breaks when called as a prefixedcommand in DM
    await ctx.send("hybrid command")


@prefixed_commands.prefixed_command(name="cmd2")
async def cmd2(ctx: prefixed_commands.PrefixedContext):
    await ctx.send("prefixed commands work")


client.start(token="YOUR_BOT_TOKEN")

Traceback

Traceback (most recent call last):
  File "/**/venv/lib/python3.11/site-packages/interactions/ext/prefixed_commands/manager.py", line 336, in _dispatch_prefixed_commands
    await command(context)
  File "/**/venv/lib/python3.11/site-packages/interactions/ext/hybrid_commands/hybrid_slash.py", line 307, in __call__
    new_ctx = context.client.hybrid.hybrid_context.from_prefixed_context(context)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/**/venv/lib/python3.11/site-packages/interactions/ext/hybrid_commands/context.py", line 119, in from_prefixed_context
    elif ctx.channel.type in {10, 11, 12}:  # it's a thread
         ^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'type'

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.
  • I have attempted to debug this myself, and I believe this issue is with the library

Additional Information

Setting up a PrefixedCommand with the same setup results in no issues (see the example code). I'm not very familiar with the library's internals, but as I understand it, the following is happening to create this problem:

  1. We generate a PrefixedContext using the message invoking the command
  2. HybridContext tries to access PrefixedContext.channel, which in turn tries to get the DMChannel from the client cache
  3. However, since the DMChannel is not cached, .channel is None
  4. Accessing .channel.type then results in an AttributeError

I don't know if app permissions are inferred from a channel anywhere else in the library, but that might be the way to fixing this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions