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

问题描述

限时删除!!

我正在制作一个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在Channel中交谈,它将不断给出答复,我想限制这一点.例如,在discord.py命令中,使用 @ commands.cooldown(1,3600,命令.BucketType.user)很容易,但是我所拥有的不是命令,因此不能在此处使用

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.

我以为我可以让机器人睡觉(等待asyncio),当它注意到其中一个用户在CHANNEL中显示一条消息时,然后当该人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

但是因为它再次运行,它再次重置为message = 1,并且如果我将message = 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.

推荐答案

如果您的意图纯粹是为了使Bot适时冷却,那么您可以执行以下操作:

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()

这是在嵌齿轮中完成的,因此,如果要在嵌齿轮外使用它,可能必须进行更改.如果您只想计算消息数,则只需一个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机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 08:20