twitch.py

...

import random import time from twitchio import Client, Message from twitchio.ext import commands

Set up bot token and channel

TWITCH_OAUTH_TOKEN = "oauth:your_oauth_token" TWITCH_CHANNEL_NAME = "your_channel_name"

Fun responses to random chat

ROBOTIC_HUMOR_RESPONSES = [ "Beep boop, I'm a bot. Ask me again in 5 minutes.", "I'm calculating pi to 10,000 digits, ask me later.", "My circuits are fried. Please send a support ticket.", "I am the future, I am the now, but right now, I am quite tired.", "Beep boop! Just making sure you're human... You passed! Congrats!", "If I had a dollar for every time I was asked that... I'd still be a bot.", "404 humor not found. Please try again.", "Why did the bot cross the road? To debug the chicken on the other side.", ]

Create bot class

class TwitchBot(commands.Bot): def init(self): super().init(token=TWITCH_OAUTH_TOKEN, prefix="!", initial_channels=[TWITCH_CHANNEL_NAME])

async def event_ready(self):
    """Called once when the bot is ready"""
    print(f"Logged in as {self.nick}")

async def event_message(self, message: Message):
    """Called every time a message is sent in chat"""
    if message.author.name.lower() == self.nick.lower():
        return  # Ignore messages from the bot itself

    print(f"[Chat] {message.author.name}: {message.content}")

    # Respond to commands
    if message.content.startswith("!"):
        await self.handle_commands(message)

    # Random funny bot responses to general messages
    if random.random() < 0.05:  # 5% chance to respond randomly
        await message.channel.send(random.choice(ROBOTIC_HUMOR_RESPONSES))

@commands.command(name="hello")
async def hello(self, ctx):
    """Respond to !hello command"""
    response = f"Hello, {ctx.author.name}! I'm your TwitchBot, here to assist you."
    await ctx.send(response)

@commands.command(name="joke")
async def joke(self, ctx):
    """Respond to !joke command with a random joke"""
    jokes = [
        "Why don't robots ever panic? They have good processors!",
        "What did the computer do at lunchtime? Had a byte!",
        "Why did the robot go on a diet? It had too many bytes.",
        "I tried to write a joke about a broken keyboard, but it was too many keys short."
    ]
    await ctx.send(f"{ctx.author.name}, here's a joke for you: {random.choice(jokes)}")

@commands.command(name="ping")
async def ping(self, ctx):
    """Respond to !ping command"""
    await ctx.send("Pong! ๐Ÿ“")

Run the bot

if name == "main": bot = TwitchBot() bot.run()

Last updated