Best way to recognize a command that doesn't have a prefix?

(SORTED, See below EDIT)

This code, which I have inside my component, works, but it also prints an error.

# We use a listener in our Component to display the messages received.
    @commands.Component.listener()
    async def event_message(self, payload: twitchio.ChatMessage) -> None:
        print(f"[{payload.broadcaster.name}] - {payload.chatter.name}: {payload.text}")
        
		# Check for commands without a prefix, and add the prefix if found, so it gets picked up by the command handler

        words = payload.text.lower().strip().split()	#get array of words in the message (in lowercase, whitespace before and after removed)

        if words and words[0] in (self.cmd_handler.get_no_prefix_commands()):	#if not an empty string AND the first word is one of our no-prefix commands
            # Pretend the message had the prefix
            # (edit the message and add the prefix before Twitch's command checker sees it)
            new_message_w_prefix = f"{os.getenv('PREFIX')}{words[0]} {' '.join(words[1:])}"
            log.debug(new_message_w_prefix)
            payload.text = new_message_w_prefix

            # Only process this rewritten message
            ctx = await self.bot._get_context(payload)  # current internal method for context
            await words[0].invoke(ctx)  # manually run the command

I’m getting the error:

[…]in event_message
ctx = await self.bot._get_context(payload) # current internal method for context
^^^^^^^^^^^^^^^^^^^^^
AttributeError: ‘Bot’ object has no attribute ‘_get_context’. Did you mean: ‘get_context’?

If I use the function it suggests instead, the program no longer recognizes the non-prefixed command and instead throws the error:

[…]in event_message
ctx = await self.bot.get_context(payload) # current internal method for context
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: object Context can’t be used in ‘await’ expression

What’s haunting me, is that a short while this code was magically working without the last two lines of code after the comment # Only process this rewritten message. It was somehow being called from just me updating payload.text. I was working on unrelated functionality when this stopped working, so I’m not sure what I broke.

Is there a more predicable way to tell twitchio to evaluate the message after I’ve edited it?

EDIT: FIXED IT!

I’ve changed the last to line to as follows:

#get the context of the updated payload, and use it to manually invoke the command now that it's recognizable to twitchio
ctx = self.bot.get_context(payload)
await self.bot.invoke(ctx)

So the full function (located in my component) is now:

# We use a listener in our Component to display the messages received.
    @commands.Component.listener()
    async def event_message(self, payload: twitchio.ChatMessage) -> None:
        print(f"[{payload.broadcaster.name}] - {payload.chatter.name}: {payload.text}")
        
		# Check for commands without a prefix, and add the prefix if found, so it gets picked up by the command handler

        words = payload.text.lower().strip().split()	#get array of words in the message (in lowercase, whitespace before and after removed)

        if words and words[0] in (self.cmd_handler.get_no_prefix_commands()):	#if not an empty string AND the first word is one of our no-prefix commands
            # Pretend the message had the prefix
            # (edit the message and add the prefix before Twitch's command checker sees it)
            new_message_w_prefix = f"{os.getenv('PREFIX')}{words[0]} {' '.join(words[1:])}"
            log.debug(new_message_w_prefix)
            payload.text = new_message_w_prefix

            #get the context of the updated payload, and use it to manually invoke the command now that it's recognizable to twitchio
            ctx = self.bot.get_context(payload)
            await self.bot.invoke(ctx)

No more errors and successfully calls commands without prefixes.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.