0
votes

So basically what I want is whenever my bot joins any guild it should send a message to a particular channel in my server stating that it is invited to a server with its name and if possible also the invite link of that server. I tried a few things but they never really worked.

@client.event
async def on_guild_join(guild):
    channel = client.get_channel('736984092368830468')
    await channel.send(f"Bot was added to {guild.name}")

It didn't work and throws the following error:

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "C:\Users\Rohit\AppData\Roaming\Python\Python37\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\Rohit\Desktop\discord bots\test bot\main.py", line 70, in on_guild_join
    await channel.send(f"Bot was added to {guild.name}")
AttributeError: 'NoneType' object has no attribute 'send'

And I really don't have any idea how to make the bot send invite link of the guild with the guild name.

2
Try channel = client.get_channel(736984092368830468) with channel as an integer.ThRnk
doesn't work @ThRnk Have already tried thatuser13878676

2 Answers

2
votes

The reason why you get the 'NoneType' object has no attribute 'send' exception is because your bot failed to find the channel provided.

This line:

channel = client.get_channel('736984092368830468')

Will not work, this is because the channel ID must be an integer, you can try this:

channel = client.get_channel(int(736984092368830468))

If this still does not work, make sure the bot has access to the channel, the channel exists and the ID provided is correct.

0
votes

Here's how you would get an invite, name and guild icon assuming your bot has the required permissions.

@client.event
async def on_guild_join(guild):
    channel = client.get_channel(745056821777006762)
    invite = await guild.system_channel.create_invite()

    e = discord.Embed(title="I've joined a server.")
    e.add_field(name="Server Name", value=guild.name, inline=False)
    e.add_field(name="Invite Link", value=invite, inline=False)
    e.set_thumbnail(url=guild.icon_url)
    await channel.send(embed=e)

enter image description here