Skip to content

Interacting_With_Chat_In_Commands

sharkbound edited this page Sep 7, 2022 · 8 revisions

Index

Sending Messages To Chat

Sending a Basic Message

msg.reply() is used to send messages to twitch chat the command was called from

example:

from twitchbot import Message, Command


@Command(name='basic')
async def cmd_basic(msg: Message):
    # note: msg.mention is the message authors name prefixed with an @
    # ex: johndoe: !basic
    # will say this to chat: `hello there @johndoe!`
    await msg.reply(f'hello there {msg.mention}!')

This will send the follow message to twitch chat:

hello there @johndoe!

BACK TO INDEX

Sending Chat Message as a Twitch Reply

Twitch Replies reference the original message via a small clickable button next to the message in twitch chat.

This can be useful when the command is often being called from a more active chat

example:

from twitchbot import Message, Command


@Command(name='astwitchreply')
async def cmd_astwitchreply(msg: Message):
    await msg.reply('check me in twitch chat to see who called me!', as_twitch_reply=True)    

BACK TO INDEX

Waiting for Replies in a Command

msg.wait_for_reply() waits for the bot to receive a message matching the predicate passed to the method

example:

from twitchbot import (
    Message,
    Command,
    raise_invalid_arguments_error_if_falsy,
    same_author_and_channel_predicate,
    cast_value_to_type
)


@Command(name='reply')
async def cmd_reply(msg: Message):
    await msg.reply('enter a number to echo:')

    reply = await msg.wait_for_reply(predicate=same_author_and_channel_predicate(msg))
    cast = cast_value_to_type(reply.content, int)

    # note: `raise_invalid_arguments_error_if_falsy` was added in 2.7.5
    raise_invalid_arguments_error_if_falsy(
        cast.is_cast_successful,
        f'{reply.content} must be a valid int',
        cmd=cmd_reply
    )

    await msg.reply(f'I got this reply: {cast.casted_value}')

this is how it will look in chat:

johndoe: !reply
bot: enter a number to echo:
johndoe: 42
bot: I got this reply: 42

and if it cast fails (aka user didn't enter a valid number):

johndoe: !reply
bot: enter a number to echo:
johndoe: notanumber
bot: notanumber must be a valid int - syntax: "" - do "?help reply" for more details 

BACK TO INDEX

Clone this wiki locally