0

I'm trying to send a dm to a specific user with the command .dmtest, but whenever I run the command I get the error mentioned in the title Here is my code:

import discord

from discord.ext import commands
import os
import requests
import asyncio



from keep_alive import keep_alive

client = discord.Client()

@client.event
async def on_ready():
  print('We have logged in as {0.user}'.format(client))



@client.event
async def on_message(message):
  if message.author == client.user:
    return

  if message.content.startswith('.dmtest'):
    user = client.get_user(384185404510961664)
    # await message.author.send(...)
    await message.channel.send('test worked')
    await user.send('a')

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

Error:

line 78, in on_message
  await user.send('a')
AttributeError: 'NoneType' object has no attribute 'send'
1

1 Answer 1

2

After playing around with your code a bit, I found that I got DMed "a" after replacing await user.send('a') with await message.author.send('a'). After making that change, the line user = client.get_user(384185404510961664) is unnecessary, and can be deleted.

EDIT: Since this isn't what you're looking for, I dug around the internet and found that you just needed to replace client.get_user(id) with await client.fetch_user(id).

Sign up to request clarification or add additional context in comments.

4 Comments

The thing is I want to be able to dm a specific user, instead of the person who ran the command. My next plan (once I can get this to work) is to have an input for the user to choose the ID of the person they want to dm.
Alright. I'm fairly new to discord.py myself, so I'll go through the docs myself. I understand you're trying to send a dm to the user with the ID 384185404510961664?
well for the moment but I want to change it later on so you can choose who to DM
Sorry for the late response. I updated the answer with something that works for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.