[英]How do I resend user input after a command (e.g. User: !send 1234 Bot: 1234) in discord.py?
I have recently started programming my own discord bot as many other people are doing... So far what I have got is this:我最近开始编写自己的 discord 机器人,就像许多其他人正在做的那样……到目前为止,我得到的是:
@bot.command
async def on_message(message):
if message.content.startswith("t!send"):
await client.send_message(message.content)
It doesn't crash but it doesn't work either...它不会崩溃,但它也不起作用......
It seems as if you're using a tutorial for an old version of discord.py.好像您正在使用旧版本 discord.py 的教程。 There are some big changes in the most recent version - rewrite.
最新版本有一些大的变化——重写。 Please take some time to look for more updated tutorials, or read the documentation linked above.
请花一些时间寻找更多更新的教程,或阅读上面链接的文档。
Here are the cases for using both the on_message
event and commands:以下是同时使用
on_message
事件和命令的情况:
@bot.command()
async def send(ctx, *, sentence):
await ctx.send(sentence)
######################################
@bot.event
async def on_message(message):
args = message.content.split(" ")[1:]
if message.content.startswith("t!send"):
await message.channel.send(" ".join(args))
else:
await bot.process_commands(message) # only add this if you're also using command decorators
References:参考:
commands.Context()
- if not familiar with using command decorators commands.Context()
- 如果不熟悉使用命令装饰器discord.on_message()
Bot.process_commands()
So first of all, on_message doesn't belong here and you also don't have to use it.所以首先,on_message 不属于这里,你也不必使用它。 (
on_message()
would use the decorator @bot.event
) Assuming you have setup a prefix for your bot using bot = commands.Bot(command_prefix = 't!')
you could do something like this: (
on_message()
将使用装饰器@bot.event
)假设您已经使用bot = commands.Bot(command_prefix = 't!')
为您的机器人设置了前缀,您可以执行以下操作:
@bot.command()
async def send(ctx, *args):
message = " ".join(args)
await ctx.send(message)
*args is everything the user types in after t.send: So for example: if the user types in t!send Hello world! *args 是用户在 t.send 之后输入的所有内容: 例如:如果用户输入 t!send Hello world! args would be
["Hello", "world!"]
. args 将是
["Hello", "world!"]
。 With .join
we can join them together into a single string ( message
)使用
.join
我们可以将它们连接成一个字符串( message
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.