#list index out range

1 messages · Page 1 of 1 (latest)

radiant parcel
#

Code:

@bot.event
async def on_guild_join(guild):
    server = await bot.fetch_guild(guild.id)
    print(server.channels)
    hi = await server.channels[0].create_invite()
    channel = bot.get_channel(988031087424983090)
    stuff = guild.owner
    jem = discord.Embed(color=0x2f3136)
    jem.set_author(f"{bot.user} has joined a new server:",icon_url=bot.avatar.url)
    jem.set_description(f"Server owner ID: {guild.owner_id}\
                        Server owner tag: {stuff.tag}\
                        Server name: {guild.name}\
                        Server ID: {guild.id}\
                        Server member count: {guild.member_count}\
                        Server invite: {hi}")
    await channel.send(embed=jem)

Error (full trace back):

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.9/site-packages/discord/client.py", line 382, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/main.py", line 37, in on_guild_join
    hi = await server.channels[0].create_invite()
IndexError: list index out of range
bold zenith
#

Hey, so as per before, I know you mentioned you print (server.channels), but are you getting a proper list? Obviously don't need or want you to post the output, but the verification would help.

Asking because the fact you're getting list index out of range when trying to access the first item in the list, the first thing that comes to mind is that it's an empty list.

umbral kestrel
#

Why fetch the guild when you have the guild object that you already use

bold zenith
umbral kestrel
#

what

bold zenith
#

It was a compliment? You've never heard that expression before? Basically means "that's why you're higher up"
nevermind forget it. lol

umbral kestrel
#

Oh well i haven't really heard that expression before, but thanks i guess :)

radiant parcel
radiant parcel
#

meaning it was gonna send a list, but it’s empty.. although my server does actually have channels, so slightly confused? Thonk

#

just tried creating more channels, same error

#

somehow it’s having issues fetching the channel, so not sure what’s going on there?

bold zenith
#

just throwing this out there, but what if you changed it to text_channels?

#

would only give channels that are not voice, announcement, or rules, but should work?

#

see if you still get an empty list.

#

Unless you're only getting an empty list for a single server?

radiant parcel
radiant parcel
bold zenith
#

try this

#
@bot.event
async def on_guild_join(guild):
  chan_list = []
  for channel in guild.text_channels:
    chan_list.append(channel)
  print(f'Testing Channel output: {chan_list}')
  await chan_list[0].create_invite()
  channel = bot.get_channel(988031087424983090)
  stuff = guild.owner
  jem = discord.Embed(color=0x2f3136)
  jem.set_author(f"{bot.user} has joined a new server:",icon_url=bot.avatar.url)
  jem.set_description(f"Server owner ID: {guild.owner_id}\
                      Server owner tag: {stuff.tag}\
                      Server name: {guild.name}\
                      Server ID: {guild.id}\
                      Server member count: {guild.member_count}\
                      Server invite: {hi}")
  await channel.send(embed=jem)
#

if it works, just kill the console print.

#

Basically you're creating a new list, populating it with your channels, then pulling the first one.

radiant parcel
#

sorry only just saw this

radiant parcel
radiant parcel
# bold zenith ``` @bot.event async def on_guild_join(guild): chan_list = [] for channel in...
Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "/home/container/.local/lib/python3.9/site-packages/discord/client.py", line 382, in _run_event
    await coro(*args, **kwargs)
  File "/home/container/main.py", line 38, in on_guild_join
    print(f'Testing Channel output: {chan_list}')
  File "/home/container/.local/lib/python3.9/site-packages/discord/channel.py", line 190, in __repr__
    attrs = [(val, getattr(self, val)) for val in self._repr_attrs]
  File "/home/container/.local/lib/python3.9/site-packages/discord/channel.py", line 190, in <listcomp>
    attrs = [(val, getattr(self, val)) for val in self._repr_attrs]
AttributeError: 'TextChannel' object has no attribute 'news'
bold zenith
#

Odd that THAT line it flagged

#

Comment out the print statement

#

Did you copy as I did?

#

Share please

radiant parcel
#

just fixed some indents

bold zenith
#

The indents were fine

radiant parcel
#

they were flagged as red for me

bold zenith
#

Can you share your final draft?

radiant parcel
#

and caused some errors

radiant parcel
bold zenith
#

Yes sir

radiant parcel
#
@bot.event
async def on_guild_join(guild):
    chan_list = []
    for channel in guild.text_channels:
        chan_list.append(channel)
        print(f'Testing Channel output: {chan_list}')
        hi = await chan_list[0].create_invite()
        channel = bot.get_channel(988031087424983090)
        stuff = guild.owner
        jem = discord.Embed(color=0x2f3136)
        jem.set_author(f"{bot.user} has joined a new server:",icon_url=bot.avatar.url)
        jem.set_description(f"Server owner ID: {guild.owner_id}\
                      Server owner tag: {stuff.tag}\
                      Server name: {guild.name}\
                      Server ID: {guild.id}\
                      Server member count: {guild.member_count}\
                      Server invite: {hi}")
    await channel.send(embed=jem)
bold zenith
#

not correct

#

within the "for" statement, you're ONLY appending.

#

thats why I had the indent stop

#

You're not doing all that and creating embeds for every channel.

#

which is what your code is trying to do

#

print( should be at the same level as "for"

#

as well as everything below it.

#

make more sense?

#

You're not printing until the for loop has closed

radiant parcel
#

ahhh I see

bold zenith
#

For each channel, add the channel to the list, then when done and list completed, move along, is how its meant to run.

radiant parcel
#

like that?

#

no nvm

radiant parcel
#

exactly

#

didn’t edit a thing

#

and got the same error

bold zenith
#

Comment out the print line

#

That’s where it’s failing. No idea why but it’s clear as day

#

Think I found the issue @radiant parcel

#

What pycord version are you running?

#

News attribute was committed about a month ago, I found. So if you are running anything less than RC1 you will get this problem

#

Someone closer to developers might have to troubleshoot this, if you are on RC1 already

radiant parcel
#

commented it out

bold zenith
#

Yeah, very odd

radiant parcel
#

fixed the error

bold zenith
#

Nice!

radiant parcel
#

now I have this one

#

unsure how I get the bot avatar url?

bold zenith
#

That, I have no idea tbh. Never needed that. Want the guys email too? Lol. Kidding of course.

radiant parcel
#

😂

bold zenith
#

Maybe guild instead of bot?

radiant parcel
#

thank you for fixing the other error though, hope I never have it again, will be sure to come back to you if I have an error… again, thank you a lot AHYES

radiant parcel
bold zenith
#

Oh. Lol

#

Yeah, I’ve stumped me on that one

radiant parcel
#

haha, no worries, i appreciate all the help though :D

bold zenith
#

I mean, you could just use the URL?

radiant parcel
#

hold on

#

my bot doesn’t even have a pfp

#

that would make sense why I’m getting these errors

#

💀