0

I am trying to dm everyone in the server when the bot is added to the server using the on_join_guild(). The code for it

@client.event
async def on_guild_join(guild):
    for member in guild.members:
        await member.create_dm()
        embedVar = discord.Embed(title="Hi", description="Hello", color=0x00ff00)
        await member.dm_channel.send(embed=embedVar)

But whenever I add the bot to server, it dms everyone in the server which is expected but a error also pops in the console.

Ignoring exception in on_guild_join
Traceback (most recent call last):
  File "C:\Users\Khusi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File ".\time-turner.py", line 42, in on_guild_join
    await member.create_dm()
  File "C:\Users\Khusi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\discord\member.py", line 110, in general
    return getattr(self._user, x)(*args, **kwargs)
AttributeError: 'ClientUser' object has no attribute 'create_dm'

What am I doing wrong?

SHIVANSHU SAHOO
  • 348
  • 1
  • 3
  • 11

1 Answers1

1

After replicating your code, I found that your error only appears to happen if the bot tries to message itself. You would also get an error if your bot attempts to message another bot (Cannot send messages to this user). You can just use pass if your bot can't message a user or if it tries to message itself.

@client.event
async def on_guild_join(ctx):
    for member in ctx.guild.members:
        try:
            await member.create_dm()
            embedVar = discord.Embed(title="Hi", description="Hello", color=0x00ff00)
            await member.dm_channel.send(embed=embedVar)
        except:
            pass

If the above still doesn't work, just as NerdGuyAhmad had said, you need to enable intents, at the very least member intents. View this link here: How do I get intents to work?

Bagle
  • 2,326
  • 3
  • 12
  • 36