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

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

Python Discord Bot:如何與用戶交互?

Python Discord Bot:如何與用戶交互?

PIPIONE 2023-07-05 15:37:42
我正在嘗試為我的服務器制作一個 Discord 機器人,但遇到了一些困難。我查看了其他人的問題,應用了所有類型的更改,但我仍然陷入困境。作為參考,我對 Python 比較陌生,并且 100% 是 Discord 機器人的初學者。所以,這是我的代碼:import discordfrom discord.ext import commandsprefix = ">"client = commands.Bot(command_prefix=prefix, case_insensitive=True)@client.eventasync def on_ready():    print('We have logged in as {0.user}'.format(client))@client.eventasync def on_message(message):    if message.author == client.user:        return    if message.content.startswith('>hello'):        msg = 'Hello, {0.author.mention}!'.format(message)        await message.channel.send(msg)        @client.command(name = "pomodoro")async def Pomodoro(ctx):    if ctx.content.startswith('>pomodoro'):        await ctx.channel.send("Let's grab some tomatoes! For how many minutes?")    def check(msg):        return msg.author == ctx.author and msg.channel == ctx.channel and \               type(msg.content)==int    msg = await client.wait_for("message", check=check)hello 函數完美運行。我的問題是番茄工作法(當然,它還沒有完成)。我使用此功能的目的是詢問用戶他們想要學習多少分鐘,然后詢問他們想要休息多少分鐘,然后使用這兩個變量設置一個計時器。但我什至無法讓它發送第一條消息("Let's grab some tomatoes! For how many minutes?")。我不知道我做錯了什么,特別是當第一個功能工作正常時。提前致謝!
查看完整描述

1 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

覆蓋提供的默認值on_message會禁止運行任何額外的命令。要解決此問題,請client.process_commands(message)在on_message.


@client.event

async def on_message(message):

    if message.author == client.user:

        return


    if message.content.startswith('>hello'):

        msg = 'Hello, {0.author.mention}!'.format(message)

        await message.channel.send(msg)


    await client.process_commands(message)  # <----



@client.command(name="pomodoro")

async def _pomodoro(ctx):

    await ctx.channel.send("Let's grab some tomatoes! For how many minutes?")


    def check(msg):

        return msg.author == ctx.author and msg.channel == ctx.channel and \

               type(msg.content) == int


    msg = await client.wait_for("message", check=check)


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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