如何限制 on_message 回复(Discord Python 机器人)

时间:2023-05-12
本文介绍了如何限制 on_message 回复(Discord Python 机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在制作一个 Discord Bot,它可以做各种事情,包括对提到该机器人的人做出反应,以及对某些用户在某个频道中所说的话做出反应.我的代码(为清楚起见而缩短)是:

I am making a Discord Bot that does various things, including reacting to the person who mentions the bot and reacting to things certain users say in a certain channel. My code (shortened for clarity) is:

BOT = os.getenv('DISCORD_BOT_MENTION') 
CHANNEL = os.getenv('DISCORD_CHANNEL')
USER1 = os.getenv('DISCORD_USER1_ID')
USER2 = os.getenv('DISCORD_USER2_ID')

@bot.event 
async def on_message(message):  
    content = message.content # Did bot get mentioned?
    channel = message.channel # Where was the bot mentioned
    channel_ID = message.channel.id #
    user = message.author.name # Who mentioned the bot
    user_ID = message.author.id

    if user == client.user:
        return

    elif BOT in content:
        brankos_responses = [
            "Hello",
            "Hi",
        ]
        weights_responses = [
            15,
            10,
        ]
        response = random.choices(brankos_responses, weights_responses, k = 1)
        await asyncio.sleep(0.7) # Wait 0.7s before answering
        await channel.send(response[0])
    
    elif channel_ID == int(CHANNEL):
        if user_ID == int(USER_ID_1): 
            brankos_responses = [
                "Test Message 1 for User 1 in Channel",
                "Test Message 2 for User 1 in Channel",
            ]
            response = random.choice(brankos_responses)
            await channel.send(response)

        if user_ID == int(USER_id_2):
            brankos_responses = [
                "Test Message 1 for User 2 In Channel",
                "Test Message 2 for User 2 in Channel",
            ]
            response = random.choice(brankos_responses)
            await channel.send(response)

    else: 
        return

    await bot.process_commands(message)

但是我发现,如果您向机器人发送垃圾邮件,或者如果用户 1 和 2 在频道中交谈,它会继续回复,我想限制这种情况.例如,在使用 @commands.cooldown(1, 3600, commands.BucketType.user) 很容易的 discord.py 命令中,但是我拥有的不是命令,因此不能在这里使用.

However I found that if you spam @ the bot or if the Users 1 and 2 talk in Channel, it will keep giving replies, and I would like to limit that. In discord.py commands that's easy with @commands.cooldown(1, 3600, commands.BucketType.user) for example, however what I have is not a command, so it can't be used here.

我认为我可以让机器人休眠(使用 await asyncio),当它注意到频道中来自一位用户的消息时,然后当该人 5 秒钟内没有说任何话时,它会运行代码并发送 1 个回复,但这只会导致所有回复堆积起来,并在几秒钟后发送.

I thought that I could just have the bot sleep (with await asyncio), when it notices a message in the CHANNEL from one of the users, and then when the person has not said anything for 5 seconds that it would run the code and send 1 reply, however that just results in all the responses piling up and all being send after a few seconds.

所以我的问题是:如何限制机器人的回复数量?

So my question is: How can I limit the amount of replies the bot gives?

现在我正在尝试这个:

        user_list = [
            int(USER1_ID),
            int(USER2_ID),
            int(USER3_ID),
            int(USER4_ID),
        ]

        messages = 0 # Define the count
        
        if user_ID in user_list:
            messages += 1 # Add one to count
            print(messages)

            if messages > 5: # If messages is more than 5, execute code
                response = random.choice(brankos_responses)
                await channel.send(response)
                messages = 0 # Reset count

但是因为它再次运行,它再次重置为messages = 1,如果我将messages = 0放在函数之外它不起作用.

But because it runs again, it resets to messages = 1 again, and if I put messages = 0 outside the function it does not work.

推荐答案

如果你的目的纯粹是为了让机器人在时间上有一个冷却时间,那么你可以这样做:

If your intention is purely to have a cooldown on the bot timewise, then you could do something such as:

class StackOverflow(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.last_timeStamp = datetime.datetime.utcfromtimestamp(0)

    @commands.Cog.listener()
    async def on_message(self, message):
        time_difference = (datetime.datetime.utcnow() - self.last_timeStamp).total_seconds()
        if time_difference < 120:
            # Don't do anything and return
            return

        else:
            #Do things
            self.last_timeStamp = datetime.datetime.utcnow()

这是在 Cog 中完成的,因此如果您想在 Cog 之外使用它,可能需要进行更改.如果您只想计算消息的数量,那么只需有一个 int 变量,每条消息递增它并更改 if 语句.

This was done in a Cog, so you might have to make alterations if you want to use it outside a Cog. If you wanted to just count the number of messages, then just have a int variable, increment it every message and change the if statement.

这篇关于如何限制 on_message 回复(Discord Python 机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:为什么我可以使用 bot.get_user 函数获得一些用户,而其他用户却不能?[不和谐.py] 下一篇:在 discord.py 中转换时间的 Tempmute 命令

相关文章