如何使我的机器人在没有on_message事件的情况下回应用户的提及?

时间:2020-07-21 06:37:54

标签: python python-3.x discord discord.py discord.py-rewrite

Blockquote

@client.command()
async def afk(ctx, activity=None):
    if ctx.author.mention:
        await ctx.send(f"""{ctx.author.mention} is currently afk. Reason: {activity}""")

    else:
        print("A user is afk...")

我正在尝试发出afk命令,这就是我所得到的。我无法让我的漫游器响应最近才使用此afk命令的用户。基本上,我正在尝试使Dyno bot的afk命令。

"The AFK module allows users to set an AFK status inside of your server. If a user sets an AFK status, Dyno will leave a message displaying their AFK status whenever the user is mentioned. Dyno will automatically add “[AFK]” to the beginning of your nickname whenever you set an AFK status (if it has the permissions to)."

这是Dyno的afk命令的定义。基本上,这也是我想要做的。

1 个答案:

答案 0 :(得分:2)

您可能试图在不了解实际库的情况下复制其他创建者。您需要使用自己的逻辑并理解概念。

我不知道dyno的工作原理,但是,我已经理解了您实际上要说的内容,该代码段可以使您了解事物的工作方式以及如何在代码中实现它们。

#STORING THE USER IN A LIST
afk_list = []
@bot.command()
async def afk(ctx):
    global afk_list
    # IF USER ID IS NOT FOUND IN THE LIST
    if not ctx.author.id in afk_list:
        #STORE THE ID IN THE LIST
        afk_list.append(ctx.author.id)
        #EDIT THE NAME OF THE AUTHOR
        await ctx.author.edit(nick = f'[AFK] {ctx.author.name}')
        await ctx.send(f'{ctx.author.mention} You have been marked as AFK.')
        return afk_list
    else:
        afk_list.remove(ctx.author.id)
        nick = ctx.author.name.replace('[AFK]','')
        await ctx.author.edit(nick = nick)
        await ctx.send(f'{ctx.author.mention} You are no longer AFK.')
        return afk_list

@bot.event
async def on_message(message):
    global afk_list
    try:
        #We check if the message starts with any mentionable content
        if message.content.startswith('<'):
            a = message.content
            a = a.replace('<','')
            a = a.replace('@','')
            a = a.replace('!','')
            a = a.replace('>','')
            a = int(a)
            #HERE We have replaced all the strings and converted it into Integar.
            if a in afk_list:
                #If the User in the LIST, The message will be sent.
                user = bot.get_user(a)
                await message.channel.send(f'{user.name} is currently AFK')
            else:
                pass
    except Exception:
        pass
        
    await bot.process_commands(message)