1

I'm going to send someone a message from a bot, The following code snippet is what I wrote to do that when I type $sendpm it will ask for an id and then for a message to send.

if message.content == "$sendpm":
    await message.channel.send("What is the users ID?")
    userid = await bot.wait_for('message')
    print(userid.content)
    user = bot.get_user_info(userid.content)
    await message.channel.send("What would you like to send them?")
    pm = await bot.wait_for("message")
    await user.send(user, pm.content)

when I do this I get this error: AttributeError: 'Bot' object has no attribute 'get_user_info'

Javad
  • 2,033
  • 3
  • 13
  • 23
Killogee
  • 43
  • 6

3 Answers3

0

Based on your error, you will not be able to use get_user_info in this situation. get_user_info also makes an API call to discord every time instead of looking in discord.py's cache, which you shouldn't do too often either. Instead, you should use get_user or get_member. Once this error is fixed, you will find that your await user.send() line may come across an error too, which can easily be avoided by removing the first user argument. Remember to enable intents as well! Do view the revised code below.

await message.channel.send("What is the users ID?")
userid = await bot.wait_for('message')

# There are two methods to get a user since get_user_info is not a good way to do it
# Method 1: Find user within the guild itself (best method)
user = message.guild.get_user(userid.content)

# Method 2: Find user through bot (can be slower if bot is in a lot of guilds)
user = bot.get_user(userid.content)

await message.channel.send("What would you like to send them?")
pm = await bot.wait_for("message")

# You do not need to have two arguments in this situation
# as you are already 'sending' to the 'user' the 'pm.content'
await user.send(pm.content)

Code working

Helpful Links:

Bagle
  • 2,326
  • 3
  • 12
  • 36
  • when i change it everythingg looks to be ok and it will ask what i would like to send them and once i put the message i get this `AttributeError: 'NoneType' object has no attribute 'send' ` and this refuring to the `await user.send(pm.conent)` – Killogee Dec 26 '21 at 07:21
  • @Killogee I have already mentioned how to do this in my answer. Please visit this question on [How to enable member intents?](https://stackoverflow.com/a/64832812/14420546) to help you. – Bagle Dec 26 '21 at 13:44
0

You can use bot.get_user method to find user by ID:

await message.channel.send("What is the users ID?")
userid = await bot.wait_for('message')
print(userid.content)
user = bot.get_user(userid.content)
await message.channel.send("What would you like to send them?")
pm = await bot.wait_for("message")
await user.send(pm.content)
Ratery
  • 2,863
  • 1
  • 5
  • 26
0

AttributeError: 'Bot' object has no attribute 'get_user_info'

commands.Bot has no attribute called get_user_info. I'm guessing you meant get_user/get_member, which returns a User or Member object which has various attributes to get the relevant "info".

if message.content == "$sendpm":
        await message.channel.send("What is the users ID?")
        userid = await bot.wait_for('message')
        print(userid.content)
        user = bot.get_member(int(userid.content)) # Since message.content would always return a str, we have to make it an int in-order to get a valid "ID"
        await message.channel.send("What would you like to send them?")
        pm = await bot.wait_for("message")
        await user.send(pm.content)

But however, if you want bot.get_user_info, which could be a custom method, to be available globally(where you have access to your bot instance), you can create a 'bot variable'. Essentially a custom attribute in your bot instance.

bot = commands.Bot(...)

def get_user_info(user_id: int):
    user = bot.get_member(user_id)
    return user.name, user.id

bot.get_user_info = get_user_info

As a sidenote: instead of checking if message.content is equal to a user command in an on_message event, you can start using the commands framework

Yeti
  • 155
  • 10