Discord Bot中的AFK功能 [英] AFK function in Discord Bot

查看:136
本文介绍了Discord Bot中的AFK功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用python(对我来说几乎是全新的东西)在Discord中创建一个机器人.我试图做一个AFK函数(类似于Dyno的函数).这是我的代码:

I'm currently trying to make a bot in Discord using python (something almost brand new for me). I was trying to make an AFK function (similar to Dyno's). This is my code:

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状态.我知道这是有可能的,因为其他机器人也这样做了,但是我迷失了方向,无法想办法.预先感谢!

My issue here is that the user that uses this function will be AFK until they write again >afk. My intention was to make the bot able to remove the AFK status whenever the user talked again in the server, regardless they used a bot function or not. I know it's possible because other bots do so, but I'm lost and can't think of a way to do so. Thanks in advance!

推荐答案

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

I think that should do it, but you could as well pass the ctx again or save ctx as an parameter to access. But this version does not notify the user, that they are not afk anymore

这篇关于Discord Bot中的AFK功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆