[英]How do I resend user input after a command (e.g. User: !send 1234 Bot: 1234) in discord.py?
我最近開始編寫自己的 discord 機器人,就像許多其他人正在做的那樣……到目前為止,我得到的是:
@bot.command
async def on_message(message):
if message.content.startswith("t!send"):
await client.send_message(message.content)
它不會崩潰,但它也不起作用......
好像您正在使用舊版本 discord.py 的教程。 最新版本有一些大的變化——重寫。 請花一些時間尋找更多更新的教程,或閱讀上面鏈接的文檔。
以下是同時使用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
參考:
commands.Context()
- 如果不熟悉使用命令裝飾器discord.on_message()
Bot.process_commands()
所以首先,on_message 不屬於這里,你也不必使用它。 ( 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 是用戶在 t.send 之后輸入的所有內容: 例如:如果用戶輸入 t!send Hello world! args 將是["Hello", "world!"]
。 使用.join
我們可以將它們連接成一個字符串( message
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.