我一直在尝试用discord.py制作一个不和谐机器人。我知道如何使机器人发送消息到指定的用户,但我想知道,是否有一种方法来发送消息的给定次数?如果有,我该怎么做?
例如,如果用户键入!dm @discorduser hello 5,机器人将发送"hello"给指定用户5次
目前为止,我的代码是:import discord
from discord.ext import commands
client = commands.Bot(command_prefix='.')
@client.event
async def on_ready():
print('Bot is ready.')
@client.command()
async def spam(ctx, member: discord.Member, *, content):
channel = await member.create_dm()
await channel.send(content)
client.run('bot token')
这是我对这个问题的回答,根据我的理解,你试图直接向用户发送x次消息,并且x可以根据用户的需要进行更改。
@client.command()
# the arguments are: member, the amount of times you want to DM, and what you want to DM them.
async def spam(ctx, member : discord.Member, amount=1, content=None):
# this loop will keep doing the script below the amount of 'amount' times.
for i in range(amount):
channel = await member.create_dm()
await channel.send(content)