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

断了今生、忘了曾经 提交于 2019-12-08 02:10:14

问题


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?


回答1:


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




回答2:


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

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