0

I've recently tried to create a simple bot in discord with Python Code.
I'm testing just the first features to DM a user when he joins the server

Here is my code:

import os

import discord
from dotenv import load_dotenv

load_dotenv()  #load .env files

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)  

    print(
        f'{client.user} has connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )                                                            #debug
    
    members = '\n - '.join([member.name for member in guild.members])  

    print(f'Guild Members:\n - {members}')  #debug

@client.event
async def on_member_join(member):
    await member.creat_dm()
    await member.dm_channel.send(
        f'Hi {member.name}, welcome to my Discord Server!'
    )

client.run(TOKEN) 
Ignoring exception in on_member_join
Traceback (most recent call last):
  File "/home/andre/.local/lib/python3.8/site-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "/home/andre/repos/github/discord_project/bot.py", line 30, in on_member_join
    await member.creat_dm()
AttributeError: 'Member' object has no attribute 'creat_dm'

Can anyone help me with this annoying bug?
I've seen articles that show member.create_dm() being used

2 Answers 2

2

You are right, there is a member.create_dm() https://discordpy.readthedocs.io/en/latest/api.html?highlight=create_dm#discord.Member.create_dm But you spelled it wrong "member.create_dm()"

You may try to store the DM channel into a variable, so you can call it later. (Just my opinion on making the code better)

@client.event
async def on_member_join(member):
    dmChannel = await member.create_dm()
    await dmChannel.send(f'Hi {member.name}, welcome to my Discord Server!)
Sign up to request clarification or add additional context in comments.

Comments

2

The answer posted is correct but you should not call create_dm()frequently, member.send() works the most time.

Docs: create_dm()

@client.event
async def on_member_join(member):
    await member.send(f'Hi {member.name}, welcome to my Discord Server!)

Comments

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.