alex cole
alex cole

Reputation: 41

'Client' object has no attribute 'send_message' error

hello i have been trying to edit this code to get it to work but im stuck at a point where ive been getting this error:

File "main.py", line 24, in on_member_join
    await client.send_message(member, DMMessage)
AttributeError: 'Client' object has no attribute 'send_message'

i assume this has something to do with the intents but after lots of research and time i haven't been able to fix it could someone explain to me how i can fix this?

my code for reference:

import discord

import os

intents = discord.Intents(members=True, messages = True, guilds=True,)



client = discord.Client(intents=intents)

@client.event
async def on_ready():
    print('logged in as')
    print(client.user.name)
    print(client.user.id)
    print('-----')

DMMessage = "hello"

@client.event
async def on_member_join(member):
    print("Recognized that " + member.name + " joined")
    await client.send_message(member, DMMessage)
    await client.send_message(discord.Object(id='77769000355535264'), 'Welcome!')
    print("Sent message to " + member.name)
    print("Sent message about " + member.name + " to the server")

@client.event
async def on_member_remove(member):
    print("Recognized that " + member.name + " left")
    await client.send_message(discord.Object(id='77769000355535264'), '**' + member.mention + '** just left.')
    print("Sent message to server")


client.run(os.getenv('TOKEN'))

Upvotes: 0

Views: 430

Answers (1)

thegamecracks
thegamecracks

Reputation: 584

Client.send_message() is a very outdated method and has been removed in the latest versions of discord.py. Use Member.send() to send DM messages:

@client.event
async def on_member_join(member):
    await member.send('Welcome to the server!')

Also note that when a member leaves the server and the bot has no common guilds with that user, your bot will be unable to DM them and will throw a discord.Forbidden exception.

Upvotes: 1

Related Questions