How to make a bot DM a list of people? (Discord.py) (Rewrite)

喜欢而已 提交于 2019-12-06 08:05:50

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!
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!