1 回答

TA貢獻1900條經(jīng)驗 獲得超5個贊
您可以通過兩種方式實現(xiàn)所需的功能。
保存用戶發(fā)送的最后一條消息。
等待投票命令中的新選項
最后一件事更好。我將解釋如何做到這一點。
第 1 步:創(chuàng)建您的 !vote 命令
@client.commmands()
async def vote(ctx):
# logic to do some things when someone votes
第 2 步:添加waits_for選項的邏輯
我們在 上使用超時wait_for,所以它不會永遠持續(xù)下去,因為我們使用超時,我們需要捕獲它引發(fā)的異常。這是通過 try, except 完成的。我們還使用 while 循環(huán),因為這使我們能夠接收盡可能多的選項。請注意,while 循環(huán)中的條件可以更改。
@client.command()
async def vote(ctx):
# logic to do some things when someone votes
try:
# While the user inputs options
while True:
await __handle_vote_option_message(ctx)
except asyncio.TimeoutError:
# The user did not respond in time.
return
async def __handle_vote_option_message(ctx):
timeout_ = 10
message = await client.wait_for('message', check=lambda message: message.author == ctx.author,
timeout=timeout_)
if not __is_message_valid_vote_option(message):
# logic to handle incorrect vote options
else:
# Whatever you want to do with the option the user provided.
def __is_message_valid_vote_option(message):
# check if message is correct.
return message.content.startswith("option")
on_message在我看來,這種方式比用這種邏輯填充事件要好得多。由于邏輯屬于投票命令而不是on_message事件。
添加回答
舉報