I am trying to DM myself using a self bot. I am trying to use the get_user()
function in my code.
bot = commands.Bot(command_prefix='', self_bot=True)
counter = 0
userID = 695724603406024726
@bot.event
async def dm(userID):
print('Running Function')
global counter
if counter <= 0:
print('Finding user.')
counter += 1
user = bot.get_user(userID)
print('user:',user)
await user.send("Hello")
print('message sent')
return
bot.loop.create_task(dm(userID))
bot.run(token, bot=False)
Instead, I am returned with this error:
File "<ipython-input-1-90e5e962a6e9>", line 24, in dm
await user.send("Hello")
AttributeError: 'NoneType' object has no attribute 'send'
The bot can't find the user and returns a None
value. I have tested multiple ID's and am unsure what the problem is.
You could always use the coroutine client.fetch_user(id)
to get it done. get_user()
takes it from cache so when fresh, doesn't work most of the times.
In your case:
bot = commands.Bot(command_prefix='', self_bot=True)
counter = 0
userID = 695724603406024726
async def dm(userID):
print('Running Function')
global counter
if counter <= 0:
print('Finding user.')
counter += 1
user = await bot.fetch_user(userID)
print('user:',user)
await user.send("Hello")
print('message sent')
return
bot.loop.create_task(dm(userID))
bot.run(token, bot=False)```
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With