Discord.py(重写)如何使用on_message事件获得冷却时间? [英] Discord.py (Rewrite) How to get cooldowns working with on_message event?

查看:31
本文介绍了Discord.py(重写)如何使用on_message事件获得冷却时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将命令转换为on_message事件,因为在这种情况下,它节省了空间并且看起来更干净.但是我似乎不再使用@cooldown()了,因为我不得不使用命令.Cog.listener()

I've been trying to convert my commands to on_message events as in this case, it saves up space and it is cleaner to look at. However I can't seem to use @cooldown() anymore as I have to use commands.Cog.listener()

还有其他方法可以使冷却时间发挥作用吗?我的代码在下面列出

Is there any other way to get a cooldown working? My code is listed below

# Cog on_message for waifus and husbandos
    @commands.Cog.listener()
    # Cooldown NOT WORKING
    @cooldown(1, 1, BucketType.user)
    async def on_message(self, message):

        # Defining the channel and global variables
        global waifu_split_msg
        global husbando_split_msg
        channel = message.channel

        # Defining the message content in lowercase
        user_msg = message.content.lower()

        # Defining array for the list of waifus/husbando's available
        waifu_array = ["toga", "yumeko"]
        husbando_array = ["husk", "kakashi", "tamaki"]

        # If the channel that the command has been sent is in the list of accepted channels
        if str(channel) in settings.channels:

            # Surround with try/except to catch any exceptions that may occur
            try:

                # Makes sure that the user wants a random image of a waifu
                if 'w random' in user_msg:

                    # Get embed from randomWaifu() and send it to the channel
                    embed = randomWaifu(message, waifu_array)
                    await channel.send(embed=embed)

                # Makes sure that the user wants a specific image of a waifu
                elif user_msg.startswith('~w'):

                    # Define who the waifu is using string splitting
                    waifu_split_msg = user_msg.split("w ", 1)
                    w_array = str(waifu_split_msg[-1]).lower()

                    # Retrieve the image of the waifu that the user has specified
                    with open(f'images/AnimeImages/Waifus/{w_array}.txt') as file:
                        images_array = file.readlines()

                    # Get the full name of the waifu
                    full_name = Abbrev(w_array)

                    # Get the embed from a displayAnimeImage() and send it to the channel
                    embed = displayAnimeImage(images_array, message, full_name)
                    await channel.send(embed=embed)

            except FileNotFoundError as e:
                print(e)

                # Throw error message saying no waifu's could be found
                await channel.send(f"Sorry! That Waifu doesn't exist!! Try the Waifu's listed below!")

                # Send list of suitable waifu's to the channel
                nice = string.capwords(', '.join(map(str, waifu_array)))
                await channel.send(nice)

            # Surround with try/except to catch any exceptions that may occur
            try:

                # Makes sure that the user wants a random image of a husbando
                if 'h random' in user_msg:

                    # Get embed from randomHusbando() and send it to the channel
                    embed = randomHusbando(message, husbando_array)
                    await channel.send(embed=embed)

                # Makes sure that the user wants a specific image of a husbando
                elif user_msg.startswith('~h'):

                    # Define who the husbando is using string splitting
                    husbando_split_msg = user_msg.split("h ", 1)
                    h_array = str(husbando_split_msg[-1]).lower()

                    # Retrieve the image of the Husbando that the user has specified
                    with open(f'images/AnimeImages/Husbandos/{h_array}.txt') as file:
                        images_array = file.readlines()

                    # Get the full name of the husbando
                    full_name = Abbrev(h_array)

                    # Get the embed from a displayAnimeImage() and send it to the channel
                    embed = displayAnimeImage(images_array, message, full_name)
                    await channel.send(embed=embed)

            except FileNotFoundError as e:
                print(e)

                # Throw error message saying no husbando's could be found
                await channel.send(f"Sorry! That Husbando doesn't exist!! Try the Husbando's listed below!")

                # Send list of suitable Husbando's to the channel
                nice = string.capwords(', '.join(map(str, husbando_array)))
                await channel.send(nice)

        # if the message is outwith the enso-chan-commands
        else:
            # Makes sure that the user only typed ~w or ~h
            if user_msg.endswith('~w') or user_msg.endswith('~h'):
                # Send error message
                message = await channel.send(error_function())

                # Let the user read the message for 2.5 seconds
                await asyncio.sleep(2.5)
                # Delete the message
                await message.delete()

除了顶部的cooldown()对事件没有任何影响之外,该代码还可以完美地工作

The code works perfectly besides the fact that the cooldown() at the top does not have an effect on the event whatsoever

有人可以帮我提出另一种解决方案吗?

Can anyone help me come up with another solution to this?

推荐答案

您可以使用时间参数或计数参数来限制事件的使用次数.您将无法轻松地为每个用户做到这一点.如果您希望每个用户冷静一下,我强烈建议您切换回命令方法.这可能会有所帮助.如何限制on_message回复(Discord Python机器人)

You can limit the amount of times an event is used by using a time parameter or a count parameter. You won't be able to do it per user very easily. If you're wanting a cooldown per user, I would highly recommend switching back to a command approach. This may help. How can I limit the on_message replies (Discord Python bot)

这篇关于Discord.py(重写)如何使用on_message事件获得冷却时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆