如何为DM创建自动回复事件 [英] How to make a Auto Reply Event for DM's

查看:60
本文介绍了如何为DM创建自动回复事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码,但无法使其与Command一起运行,因此可以将其打开和关闭:

I have this Code but I cant get it run with the Command so I can turn it on and off:

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

我的问题是,当我将事件设置为"on"时,该事件未在监听.

My Problem is that the Event isn't listening when I set it to "on".

推荐答案

由于从未编辑 auto_dm 变量,因此永远不会 on ,您需要使用< autoreply 命令中的code> global 关键字以对其进行更改

It's never on cause you're not editing the auto_dm variable, you need to use the global keyword in the autoreply command in order to change it

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

您的代码也有几处错误

  1. 事件 齿轮没有 __ init __ 方法
  2. 您无需在 on_message 事件中进行while循环,这没有任何用途
  3. 您应该使用布尔值而不是 on/off
  4. 您不需要 else:pass ,只需不要输入
  1. The Events cog doesn't have an __init__ method
  2. You don't need a while loop in the on_message event, there's no purpose for that
  3. You should use booleans instead of on/off
  4. You don't need the else: pass, simply don't put it

这是固定的齿轮

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}")

这篇关于如何为DM创建自动回复事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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