消息记录,参数不正确

时间:2020-05-25 21:56:30

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

我正在尝试建立一个自动记录系统,该系统可以放置所有已发送和已删除的消息,但偶尔会出现此错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Python38\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "D:\Documents\Bots\DS BOT\cog\logger.py", line 25, in on_message
    print(f"User: {c.author} said --- {c.content} --- in #{c.channel}");
OSError: [WinError 87] The parameter is incorrect

它完全破坏了整个过程并导致错误循环,每次遇到此问题时重新启动程序都是非常烦人的

我把命令放在齿轮上

这是我的代码:

from discord.ext import commands


# https://youtu.be/BoVOzm1uhUA
class logger(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(self, c):
        if(c.guild):
            if c.guild.name == "Sniper's lounge":
                if c.author.bot == True:
                    print(f"Bot: {c.author} said --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
                else:
                    print(f"User: {c.author} said --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
            elif c.guild.name == "New GAR":
                if c.author.bot == True:
                    print(f"Bot: {c.author} said --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
                else:
                    print(f"User: {c.author} said --- {c.clean_content} --- in #{c.channel.name}");          
                    pass;
        else:
            pass;

    @commands.Cog.listener()
    async def on_message_delete(self, c):
        if(c.guild):
            if c.guild.name == "Sniper's lounge":
                if c.author.bot == True:
                    print(f"Bot: {c.author} deleted --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
                else:
                    print(f"User: {c.author} deleted --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
            elif c.guild.name == "New GAR":
                if c.author.bot == True:
                    print(f"Bot: {c.author} deleted --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
                else:
                    print(f"User: {c.author} deleted --- {c.clean_content} --- in #{c.channel.name}");
                    pass;
        else:
            pass;


#print(f"{c.created_at} || Bot: {c.author} deleted --- {c.clean_content} --- in #{c.channel.name}");

def setup(bot):
    bot.add_cog(logger(bot))

1 个答案:

答案 0 :(得分:0)

我对discord.py不太熟悉,但是,如果您只是尝试运行它,并且没有让所有日志都完美记录就可以了,只需使用以下方法即可:

try:
    print(f"User: {c.author} said --- {c.content} --- in #{c.channel}");
except OSError:
    print('An error occurred')