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'
CodePudding user response:
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
Helpful Links:
- How do I send a DM to anyone I want through a command (Old New method) - SO
- Send DM to specific User ID - SO
- How to get Member object from user id discord.py (INTENTS) - SO
CodePudding user response:
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)
CodePudding user response:
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