0

python 3.7.3 discord.py 1.3.4 raspberry pi 4

I'm stuck at the beginning, after everything was working.

I started over from scratch. When I use the for loop followed by my print(f'{client.user} I have no issues and the bot username prints to the terminal. When I ditch the loop and use the guild = discord.utils.get(client.guilds, name=GUILD) code, I get the following error in the terminal.

Ignoring exception in on_ready Traceback (most recent call last): File "/home/pi/.local/lib/python3.7/site-packages/discord/client.py", line 312, in _run_event await coro(*args, **kwargs) File "/home/pi/TackleBot/bot2.py", line 27, in on_ready f'{client.user} is connected to the following guild:\n' AttributeError: 'NoneType' object has no attribute 'name'

If I add a print(client.user) command directly after the guild = discord.utils.get command, it will print the user name there, and still error out below. I've spent hours combing through documentation, and this is where I'm at now. Still confused. If I comment out the for loop, I get the error. If I comment out the discord utility command, it works fine. Never changing anything with the print(f'{client.user} block.

I'm learning as I go, any help or advice is immensely appreciated. Thank you!

    import os
    
    import discord

    from dotenv import load_dotenv

    load_dotenv()
    TOKEN = os.getenv('DISCORD_TOKEN')
    GUILD = os.getenv('DISCORD_GUILD')
    
    client = discord.Client()

    @client.event
    async def on_ready():
        guild = discord.utils.get(client.guilds, name=GUILD)
        if guild is not None:
            channel = discord.utils.get(guild.text_channels, name=GUILD)
    # when the lines 18-20 are used, line 26 throws an object type error 'none'
    # when lines 23-25 are used, there is no error
    #    for guild in client.guilds:
    #        if guild.name == GUILD:
    #            break
        print(
            f'{client.user} is connected to the following guild:\n'
            f'{guild.name}(id: {guild.id})'
        )
        
        members = '\n - '.join([member.name for member in guild.members])
        print(f'{guild.name}:\n - {members}')
    
    client.run(TOKEN)
4
  • please post the full traceback when you get the error. Commented Aug 6, 2020 at 2:41
  • Can't you revert your commits (e.g. using git bisect) to find where you broke the code? If you don't even use anything like git, I would strongly suggest you put that on your list of things to learn. Also, as a new user here, take the tour and read How to Ask. Commented Aug 6, 2020 at 6:28
  • apologies, I've added the full traceback. Commented Aug 6, 2020 at 11:43
  • I'm learning git now as well, with this issue it's isolated to this first block of code and I can make it go away by not using the 'guild = discord.utils.get' line. It's driving me crazy trying to understand why it's erroring out though. Commented Aug 6, 2020 at 11:45

1 Answer 1

2

If I understand correctly, you're trying to print the guild info in the on_ready function first, right?

You can do it like this:

client = discord.Client()
@client.event
async def on_ready():
     for guild in client.guilds:
         print(
          f'{client.user} is connected to the following guild:\n'
          f'{guild.name}(id: {guild.id})'
         )
     members = '\n - '.join([member.name for member in guild.members])
     print(f'Guild Members:\n - {members}')

Finally:

client.run(TOKEN)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Costa, yes the intent is to make sure the bot is connected to the right guild and confirm that by printing the bot user name and guild info in terminal. In an attempt to "clean up" my code, I wanted to replace the for loop with the discord utility function get. For some reason, when I do that it errors on the print client.user line of code. Arghhh. Thank you again!
It was, I just went back to using the @client.event method rather than trying to get fancy with it as a newbie lol thanks again for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.