【问题标题】:AFK function in Discord BotDiscord Bot 中的 AFK 功能
【发布时间】:2020-12-20 07:48:28
【问题描述】:

我目前正在尝试使用 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.event
async 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 状态,无论他们是否使用了机器人功能。我知道这是可能的,因为其他机器人这样做,但我迷路了,想不出办法。 提前致谢!

【问题讨论】:

    标签: python discord


    【解决方案1】:
    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

    【讨论】:

    • global afkdict 行是不必要的,因为您正在改变它而不是覆盖它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    • 2021-07-15
    • 1970-01-01
    • 2022-01-04
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多