Discord.py AFK命令

时间:2020-10-07 22:46:11

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

因此,我和一个朋友已经在处理此AFK代码,除提及外,我们已完成所有工作。该机器人应该检查是否有人提到了AFK,如果是的话,它应该发送一条消息告知他们为什么被AFK。


@client.command()
async def afk(ctx, *, reason=None):
    embed = discord.Embed(timestamp=ctx.message.created_at, color=0x00ffff)
    embed.set_footer(text="Olympia Gaming")
    post = {
        "member": ctx.message.author.id,
        "reason": reason,
        "date": str(ctx.message.created_at)
        }
        
    if reason == None:
        collection.insert_one(post)
        embed.add_field(name=f"{ctx.author.name} is now AFK", value="Reason: No reason specified.")
        await ctx.send(embed=embed)
    else: 
        collection.insert_one(post)
        embed.add_field(name=f"{ctx.author.name} is now AFK", value=f"Reason: {reason}", inline=False)
        await ctx.send(embed=embed)

@client.event
async def on_message(message):
    embed = discord.Embed(timestamp=message.created_at, description="You have been removed from AFK.", color=0x00ffff)
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        await message.channel.send(embed=embed)

上面的代码有效。

    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK")
        

    await client.process_commands(message)  

我没有收到任何错误,但是我也希望该机器人在提及它们时显示它们之所以成为afk的原因。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

这有点晚了,我不知道底部片段是否有效,但如果有效,我们开始吧。

首先我们需要在消息中找到原因。我们这样做,

#in the the chat box we need to add the to curly brackets
    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK {}")
        

    await client.process_commands(message)

接下来,我们添加原因

    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK {reason}")
        

    await client.process_commands(message)  

最后,自定义!

    if message.mentions
    results = collection.find({"member": message.author.id}) 
    for result in results:
        collection.delete_one(result)
        if message.content == result:
            await message.channel.send(f"This person is currently AFK, Reason? {reason}")
        

    await client.process_commands(message)

就这样,以后请参考discord.py docs甚至下次python docs!

Python 文档:https://docs.python.org/3/ Discord.py 文档:https://discordpy.readthedocs.io/en/latest/

我希望我有所帮助,请告诉我这是否已经修复以及任何问题!