繁体   English   中英

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

[英]How do I make my bot respond to a user mention without on_message event?

块引用

@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 机器人的 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 命令的定义。 这基本上也是我想要做的。

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

我不知道 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM