Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError in console, but correct message in chat #471

Closed
bxrbeq opened this issue Oct 3, 2024 · 2 comments
Closed

AttributeError in console, but correct message in chat #471

bxrbeq opened this issue Oct 3, 2024 · 2 comments

Comments

@bxrbeq
Copy link

bxrbeq commented Oct 3, 2024

Hi, i'm on version 2.10.0 and im getting the following error into the console:

    File "C:\Users\---\AppData\Local\Programs\Python\Python312\Lib\site-packages\twitchio\client.py", line 208, in wrapped
        await func(*args)
    File "c:\Users\---\Documents\coding\python\twitch-bot\bxrbeq-bot.py", line 207, in event_message
        user = message.author.name
               ^^^^^^^^^^^^^^^^^^^
    AttributeError: 'NoneType' object has no attribute 'name'

in

    async def event_message(self, message):
            global NICK, CLIENT_ID,TOKENTWITCH
            user = message.author.name
            message_content = message.content
            msg = message.channel
            if user != NICK:
                if ('hallo' in message_content.lower() or 'hi' in message_content.lower()):
                    await msg.send(f'Hallo {user}')

but the message in the chat still has the authors name in it:
Image

Am I doing something wrong?

Copy link

github-actions bot commented Oct 3, 2024

Hello! Thanks for the issue. If this is a general help question, for a faster response consider joining the official Discord Server

Else if you have an issue with the library please wait for someone to help you here.

@chillymosh
Copy link
Collaborator

First, do not use global variables, they are extremely bad practice, especially in an OOP framework such as TwitchIO.

I refer you to the quickstart docs, the second example: https://twitchio.dev/en/latest/quickstart.html

from twitchio.ext import commands

class Bot(commands.Bot):

    def __init__(self):
        # Initialise our Bot with our access token, prefix and a list of channels to join on boot...
        # prefix can be a callable, which returns a list of strings or a string...
        # initial_channels can also be a callable which returns a list of strings...
        super().__init__(token='ACCESS_TOKEN', prefix='?', initial_channels=['...'])

    async def event_ready(self):
        # Notify us when everything is ready!
        # We are logged in and ready to chat and use commands...
        print(f'Logged in as | {self.nick}')
        print(f'User id is | {self.user_id}')

    async def event_message(self, message):
        # Messages with echo set to True are messages sent by the bot...
        # For now we just want to ignore them...
        if message.echo:
            return

        # Print the contents of our message to console...
        print(message.content)

        # Since we have commands and are overriding the default `event_message`
        # We must let the bot know we want to handle and invoke our commands...
        await self.handle_commands(message)

I would also opt to doing this, in you case.

if any(word in message_content.lower() for word in ['hallo', 'hi']):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants