TypeError:_new_message()缺少1个必需的位置参数:'message'

时间:2018-09-14 01:02:51

标签: python python-3.x discord discord.py

嗨,我遇到TypeError,我不确定在做什么错,这里的类型错误是TypeError: _new_message() missing 1 required positional argument: 'message',我认为这意味着我需要实例化一个类实例?

这就是我所做的,您可以看到我已经在代码中添加了message = ctx.message

这是我正在使用的东西:

class antilink:
"""
Anti URL spam
"""

def __init__(self, bot):
    self.bot = bot
    self.regex = re.compile(r"<?(https?:\/\/)?(www\.)?(discord\.gg|discordapp\.com\/invite)\b([-a-zA-Z0-9/]*)>?")
    self.regex_discordme = re.compile(r"<?(https?:\/\/)?(www\.)?(discord\.me\/)\b([-a-zA-Z0-9/]*)>?")
    self.regex_url = re.compile(r'(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))')


async def _new_message(self, ctx, message):
    """Finds the message and checks it for regex"""
    server = ctx.message.server
    user = message.author
    excluded = []
    message = ctx.message
    if ctx.message.server:
        if self.regex.search(ctx.message.content) is not None or self.regex_discordme.search(ctx.message.content) is not None \
            or self.regex_url.search(ctx.message.content)is not None and ctx.message.server:

                roles = [r.name for r in user.roles]
                bot_admin = settings.get_server_admin(ctx.message.server)
                bot_mod = settings.get_server_mod(ctx.message.server)
                if ctx.message.channel.id is excluded:
                    return
                elif user.id == settings.owner:
                    return
                elif bot_admin in roles:
                    return
                elif bot_mod in roles:
                    return
                elif user.permissions_in(ctx.message.channel).manage_messages is True:
                    return
                else:
                    asyncio.sleep(0.5)
                    await self.bot.delete_message(message)


def setup(bot):
    q = antilink(bot)
    bot.add_listener(q._new_message, 'on_message')
    bot.add_cog(q)

如果有人可以在这里帮助我,我将非常感谢。

谢谢

1 个答案:

答案 0 :(得分:0)

如果您将q._new_message注册为on_message回调,则将使用相同的参数on_message调用它,这意味着一个单独的Message参数代表发送的消息。

async def _new_message(self, message):
    """Finds the message and checks it for regex"""
    server = message.server
    user = message.author
    excluded = []
    if server:
        if self.regex.search(message.content) is not None or self.regex_discordme.search(message.content) is not None \
            or self.regex_url.search(message.content)is not None and message.server:

                roles = [r.name for r in user.roles]
                bot_admin = settings.get_server_admin(message.server)
                bot_mod = settings.get_server_mod(message.server)
                if message.channel.id is excluded:
                    return
                elif user.id == settings.owner:
                    return
                elif bot_admin in roles:
                    return
                elif bot_mod in roles:
                    return
                elif user.permissions_in(message.channel).manage_messages is True:
                    return
                else:
                    asyncio.sleep(0.5)
                    await self.bot.delete_message(message)

您还可以只定义一个async def on_message(self, message)方法,add_cog机制将自动将该方法注册为该名称事件的回调。