【问题标题】:How to make a Auto Reply Event for DM's如何为 DM 制作自动回复事件
【发布时间】:2021-04-03 09:21:56
【问题描述】:

我有此代码,但我无法使用命令运行它,因此我可以打开和关闭它:

auto_dm = "off"

@bot.command()
async def autoreply(ctx, mode: str):
""" On or Off """
    await ctx.message.delete()
    auto_dm = mode


class Events(commands.Cog):
    @commands.Cog.listener()
    async def on_message(self, message):
        while auto_dm == "on":
            if isinstance(message.channel, discord.channel.DMChannel):
                if message.author.id != bot.user.id:
                    automsg = config.get("automessage")
                    await message.channel.send(automsg)
                    time = datetime.datetime.now().strftime("%H:%M")
                    print(f"{time} | {Fore.LIGHTCYAN_EX}[Event]{Fore.RESET} | Auto Reply Message send to {message.author}")
                else:
                    pass
            else:
                pass
        else:
            pass

我的问题是,当我将事件设置为“开启”时,它没有在监听。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    它永远不是on,因为您没有编辑auto_dm 变量,您需要在autoreply 命令中使用global 关键字才能更改它

    @bot.command()
    async def autoreply(ctx, mode: str):
        global auto_dm
        await ctx.message.delete()
        auto_dm = mode
    

    您的代码也有一些问题

    1. Events cog 没有 __init__ 方法
    2. on_message 事件中不需要 while 循环,这样做没有任何意义
    3. 您应该使用布尔值而不是on/off
    4. 你不需要else: pass,干脆别放了

    这是固定的齿轮

    class Events(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
    
        @commands.Cog.listener()
        async def on_message(self, message):
            if message.author == self.bot.user:
                return # Exiting if the author of the message is ourself
    
            if auto_dm == 'on': # I'd really recommend you using True/False
                if isinstance(message.channel, discord.DMChannel):
                    automsg = config.get("automessage")
                    await message.channel.send(automsg)
                    time = datetime.datetime.now().strftime("%H:%M")
                    print(f"{time} | {Fore.LIGHTCYAN_EX}[Event]{Fore.RESET} | Auto Reply Message send to {message.author}")
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      相关资源
      最近更新 更多