pythontwitchtwitch-api

Sending a message to twitch chat doesnt work


I'm creating a simple Twitch bot for personal use. I'm using twitchio.ext commands. Everything works fine, I'm connected, I'm able to print all the messages from the chat, my bot responds to commands but I'm not able to send a message to chat and I don't know why.

from twitchio.ext import commands

class Bot(commands.Bot):

    def __init__(self):
        super().__init__(token='oauth:censored', prefix='g ', nick = "nick of the bot", irc_token = "censored", initial_channels=['channel'])

    async def event_ready(self):
        print(f'Using bot as {self.nick}')

    @commands.command()
    async def test(self, ctx: commands.Context):
        await ctx.send('test')
        print("printed")

bot = Bot()

@bot.event()
async def event_message(msg):
    print(msg.author.name)
    print(msg.content)
    await bot.handle_commands(msg)

bot.run()

When I type "g test" in chat, the message "test" is not sent but the message "printed" is printed to the console. Do you know where could be the problem? Also, I would like to ask if there is any way how to send a message to chat directly without responding to a command or event.


Solution

  • Try adding the bot as a mod

    The 'nick' in the bot init() function doesn't seem to do anything the nick is linked to your IRC username.

    I'm assuming the channel in your initial_channels in the init() func isn't actually the channel you put there since you said you were able to type in the chat.

    If the channel that you are using the bot from(linked to the oauth key) is not the same channel as you are connecting to in the chat then you probably need to add the bots username as a moderator in that chat

    try /mod [bot_username] in the chat as the channel owner to do this

    Also if your bot is the same account you are sending commands from on twitch the rate limit will be reached unless they are a mod. So either make a dedicated account for your bot or mod the bot on the server