[英]How to get user input in discord.py after command
I am wondering how to get user input after the command string.我想知道如何在命令字符串之后获取用户输入。 I have this command called
p!spam
and I want it to do p!spam (message) (amount of times)
but I have no clue how to do this.我有一个名为
p!spam
的命令,我希望它执行p!spam (message) (amount of times)
,但我不知道如何执行此操作。 Can someone help me?有人能帮我吗?
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('p!spam'):
I would recommend putting the number of times the message is supposed to be sent before the message for simplicity.为简单起见,我建议将消息应该发送的次数放在消息之前。
The command syntax for the following code is:以下代码的命令语法为:
p!spam [# of times to repeat] [message]
@client.event
async def on_message(message):
if message.author.id == client.user.id:
return
if message.content.startswith('p!spam'):
parts = message.content.split(' ')
del parts[0]
number = int(parts.pop(0))
msg = ' '.join(parts)
for k in range(number):
await message.channel.send(msg)
The on_message
method isn't a command, it's an event. on_message
方法不是命令,而是事件。
Using the on_message()
event,使用
on_message()
事件,
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('p!spam'):
try:
spam = split(' ')
length = len(spam)
amount = int(spam[length - 1])
del spam[0]
del spam[length - 1]
for i in range (amount):
await message.channel.send(' '.join(spam))
except:
await message.channel.send('Sorry, something went wrong.')
await client.process_commands(ctx) # Important for commands to work
The message to call this command would look like this (Which is what you were looking for I believe):调用此命令的消息如下所示(我相信这就是您要查找的内容):
p.spam This is my spam message.
p.spam 这是我的垃圾邮件。 10
10
Another method, where the amount is at the beginning:另一种方法,金额在开头:
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('p!spam'):
try:
spam = split(' ')
amount = int(spam[1])
del spam[0]
del spam[1]
for i in range (amount):
await message.channel.send(' '.join(spam))
except:
await message.channel.send('Sorry, something went wrong.')
await client.process_commands(ctx) # Important for commands to work
The message to call this command would look like this:调用此命令的消息如下所示:
p.spam 10 This is my spam message.
p.spam 10 这是我的垃圾邮件。
Doing this with a command will be a lot easier, you can simply do this:使用命令执行此操作会容易得多,您只需执行以下操作:
@client.command
async def spam(ctx, amount, *, spam):
for i in range(int(amount)):
await ctx.send(spam)
The message to call this command would look like this:调用此命令的消息如下所示:
p.spam 10 This is my spam message.
p.spam 10 这是我的垃圾邮件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.