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

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

Discord Bot 中的 AFK 功能

Discord Bot 中的 AFK 功能

梵蒂岡之花 2023-07-05 15:36:38
我目前正在嘗試使用 python 在 Discord 中創建一個機器人(對我來說幾乎是全新的)。我試圖制作一個 AFK 功能(類似于 Dyno 的)。這是我的代碼:afkdict = {}@client.command(name = "afk", brief = "Away From Keyboard",                description = "I'll give you the afk status and if someone pings you before you come back, I'll tell "                              "them that you are not available. You can add your own afk message!")async def afk(ctx, message = "They didn't leave a message!"):    global afkdict    if ctx.message.author in afkdict:        afkdict.pop(ctx.message.author)        await ctx.send('Welcome back! You are no longer afk.')    else:        afkdict[ctx.message.author] = message        await ctx.send("You are now afk. Beware of the real world!")@client.eventasync def on_message(message):    global afkdict    for member in message.mentions:          if member != message.author:              if member in afkdict:                  afkmsg = afkdict[member]                  await message.channel.send(f"Oh noes! {member} is afk. {afkmsg}")    await client.process_commands(message)我的問題是,使用此功能的用戶將被AFK,直到他們再次寫入>afk。我的目的是讓機器人能夠在用戶再次在服務器中通話時刪除 AFK 狀態,無論他們是否使用機器人功能。我知道這是可能的,因為其他機器人也這樣做,但我迷失了方向,想不出辦法。提前致謝!
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

async def on_message(message):

    global afkdict

    if message.author in afkdict:

       afkdict.pop(message.author)


    for member in message.mentions:  

        if member != message.author:  

            if member in afkdict:  

                afkmsg = afkdict[member]  

                await message.channel.send(f"Oh noes! {member} is afk. {afkmsg}")

    await client.process_commands(message)

我認為應該這樣做,但是您也可以再次傳遞 ctx 或將 ctx 保存為參數來訪問。但這個版本并沒有通知用戶,他們不再afk了


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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