亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

discord.py 如何再次從用戶那里獲取消息?

discord.py 如何再次從用戶那里獲取消息?

撒科打諢 2023-05-16 16:56:16
在 discord.py 重寫中,我試圖制作一個投票系統。投票可能需要空格,比如!vote do this option    or do that option所以我想收到同一個用戶的 2 條消息。起初,我使用@client.commmands(),但我認為使用on_message會更好,但任何一個都可以。我在想這個,@client.eventasync def on_message(ctx): #We only get ctx because it can contain spaces    userid = ctx.author.id    @client.event    ....所以我的問題是,它是否有任何功能可以使您可以從同一用戶那里獲取內容 2 次,并且可以@client.event在async def.任何解決方案?謝謝。
查看完整描述

1 回答

?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

您可以通過兩種方式實現所需的功能。


保存用戶發送的最后一條消息。

等待投票命令中的新選項

最后一件事更好。我將解釋如何做到這一點。


第 1 步:創建您的 !vote 命令


@client.commmands()

async def vote(ctx):

    # logic to do some things when someone votes

第 2 步:添加waits_for選項的邏輯


我們在 上使用超時wait_for,所以它不會永遠持續下去,因為我們使用超時,我們需要捕獲它引發的異常。這是通過 try, except 完成的。我們還使用 while 循環,因為這使我們能夠接收盡可能多的選項。請注意,while 循環中的條件可以更改。

@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事件。


查看完整回答
反對 回復 2023-05-16
  • 1 回答
  • 0 關注
  • 197 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號