我一直在尝试用discord.py制作一个不和谐的机器人。我知道如何让机器人向指定的用户发送消息,但是我想知道,有没有办法发送给定次数的消息?如果有,我该怎么做?
例如,如果用户键入! dm@discorduser hello 5,机器人将向指定用户发送5次“hello”。
到目前为止,我的代码是:
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)