首页 > 解决方案 > 在 disocrd.py 中我的 automod 系统有问题

问题描述

因此,我正在尝试制作一个事件,如果有人发送任何邀请链接,我的 BOT 将检测、删除它并将用户静音,并在我的日志频道中发送日志告诉用户尝试发送邀请链接到目前为止,这是我的代码:

@bot.event
async def on_message(ctx, message):
    discordInviteFilter = re.compile("(...)?(?:https?://)?discord(?:(?:app)?\.com/invite|\.gg)/?[a-zA-Z0-9]+/?")
    guild = ctx.guild
    embed1=discord.Embed(title="Muted successfully", colour=red)
    embed1.set_thumbnail(url=message.member.avatar_url)
    embed1.add_field(name="Reason", value=f"Send invite link", inline=False)
    mutedRole = discord.utils.get(guild.roles, name=" | Muted")
    channel = discord.utils.get(message.member.guild.channels, name="║logs")
    if discordInviteFilter.match(message.content):
        await ctx.message.delete()
        await ctx.message.channel.send(f'{ctx.message.author.mention}, invite link are not allowed here. Please read the rules again')
    await bot.process_commands(message)
    await ctx.message.author.add_roles(mutedRole)    
    await channel.send(embed=embed1)

当我运行我的代码并发送邀请链接时,这就是我遇到的错误

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Blue\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'message'
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Blue\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'message'
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Blue\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
TypeError: on_message() missing 1 required positional argument: 'message'

这些行有什么错误吗?我需要你的帮助。非常感谢

标签: discord.py

解决方案


您正在为on_messageevent 提供 2 个参数,它只需要一个参数,即message.

删除ctx参数并尝试以下代码:

@bot.event
async def on_message(message):
    discordInviteFilter = re.compile("(...)?(?:https?://)?discord(?:(?:app)?\.com/invite|\.gg)/?[a-zA-Z0-9]+/?")
    guild = message.guild
    embed1=discord.Embed(title="Muted successfully")
    embed1.set_thumbnail(url=message.author.avatar_url)
    embed1.add_field(name="Reason", value=f"Send invite link", inline=False)
    mutedRole = discord.utils.get(guild.roles, name=" | Muted")
    channel = discord.utils.get(message.author.guild.channels, name="║logs")
    if discordInviteFilter.match(message.content):
        await message.delete()
        await message.channel.send(f'{message.author.mention}, invite link are not allowed here. Please read the rules again')
        await bot.process_commands(message)
        await message.author.add_roles(mutedRole)    
        await channel.send(embed=embed1)

推荐阅读