Home > front end >  error with notification of a new server to me in private messages
error with notification of a new server to me in private messages

Time:11-19

A couple of days ago the code worked, but now it gives an error, please help `

#оповищение о новом сервере
@client.event
async def on_guild_join( guild ):


    me = client.get_user(404915501727219723)

    emb = discord.Embed( title = f'Я пришел на новый сервер' )

    for guild in client.guilds:
        category = guild.categories[0]
        try:
            channel = category.text_channels[0]
        except:
            channel = category.voice_channels[0]
        link = await channel.create_invite()
    emb.add_field( name = guild.name, value = f"Участников: {len(guild.members)}\nСсылка: {link}" )

    await me.send( embed = emb )

`

Now it gives this error, didn't find anything on the internet. Here is the error: enter image description here

CodePudding user response:

The error is probably occuring because the first category in the guild doesn't have any channels or your bot doesn't have access to it. I would suggest looping through all channels until you find one that you can create the invite in. Also you would still need a code for the case that there are no channels your bot can create an invite in, so the code would look like this:

link = ""
for channel in guild.channels:
    try:
        link = await channel.create_invite()
    except:
        pass
    else:
        break
if link == "":
    link = "Failed to generate"

This should prevent every possibly occuring error

CodePudding user response:

According to the error message, the exception is thrown at channel = category.text_channels[0] after that the except clause was executed. It also throws list index out of range error.

The problem is that your category list is not populated with any element. I would suggest using a default value if the list has zero elements.

channel = category.text_channels[0] if not category.text_channels else YourDefaultInstance()

Modify the YourDefaultInstance() to correct the class.

  • Related