So this sends a DM to whoever I @mention.
@bot.command(pass_context=True)
async def pm(ctx, user: discord.User):
await user.send('hello')
How could I change this to message a list of IDs in let's say a text file, or a list variable containing user IDs?
In other words, how can I message multiple people with one command?
You can use Client.get_user_info
to get the User
class for a specified id value, if it exists.
Here is an example of how this can be done.
@bot.command()
async def pm(ctx):
user_id_list = [1, 2, 3] # Replace this with list of IDs
for user_id in user_id_list:
user = await bot.get_user_info(user_id)
await user.send('hello')
Also note that you do not need pass_context=True
as context is always passed in the rewrite version of discord.py
. See here: https://discordpy.readthedocs.io/en/rewrite/migrating.html#context-changes
If you want to message multiple people from the command, you can use the new Greedy
converter to consume as many of a certain type of argument as possible. This is slightly different than the *args
syntax because it allows for other arguments of different types to come after it:
from discord.ext.commands import Bot, Greedy
from discord import User
bot = Bot(command_prefix='!')
@bot.command()
async def pm(ctx, users: Greedy[User], *, message):
for user in users:
await user.send(message)
bot.run("token")
Useage:
!pm @person1 @person2 @person3 This is my message!
来源:https://stackoverflow.com/questions/53044936/how-to-make-a-bot-dm-a-list-of-people-discord-py-rewrite