Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用cogs discord.py说脏话时出错_Python_Discord_Discord.py - Fatal编程技术网

Python 使用cogs discord.py说脏话时出错

Python 使用cogs discord.py说脏话时出错,python,discord,discord.py,Python,Discord,Discord.py,我得到一个错误,wait channel.send(f“嘿,科尔!我发现**{message.author}**刚才在**{message.guild.name}**中说**{msg_content}**”。)AttributeError:'NoneType'对象没有属性'name'每当我键入一个不好的单词时,我就会得到上面的错误。虽然还是我,但上面说机器人说了坏话。我怎样才能解决这个问题?谢谢 我的代码是: @commands.Cog.listener() async def on_m

我得到一个错误,
wait channel.send(f“嘿,科尔!我发现**{message.author}**刚才在**{message.guild.name}**中说**{msg_content}**”。)AttributeError:'NoneType'对象没有属性'name'
每当我键入一个不好的单词时,我就会得到上面的错误。虽然还是我,但上面说机器人说了坏话。我怎样才能解决这个问题?谢谢

我的代码是:

@commands.Cog.listener()
    async def on_message(self, message):

      msg_content = message.content.lower()
 
      curseWord = ['bad words here']
 
      if any(word in msg_content for word in curseWord):
        await message.delete()
        embed=discord.Embed(title="No No Word", description=f"{message.author.mention}, Hey! Those words arent allowed here!", color=0x00FFFF)
        author = message.author
        pfp = author.avatar_url
        embed.set_author(name=f"{author}", icon_url=pfp)
        await message.channel.send(embed=embed)

        user_id = 467715040087244800
        user = await self.bot.fetch_user(user_id)
        channel = await user.create_dm()
        await channel.send(f"Hey Cole! I caught **{message.author}** saying **{msg_content}** in **{message.guild.name}** just now.")

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

你遇到了逻辑问题

因为您的代码第一次工作,所以您得到了DM。你的机器人发送给你的DM的消息也会触发一个新的
on_消息事件。由于bot在你的DM中重复被禁止的单词,你的bot会被自己的垃圾邮件过滤器捕获,并希望向你发送另一个DM。顺便说一句,如果属性错误没有发生,这将导致无限循环。
这也解释了为什么会发生: 第二次您的
on_message
事件运行时,它是由您自己的bot在您的DM中触发的,因此
message.guild
将为
None
。为了克服这个问题,您可以忽略DM聊天中的消息

@commands.Cog.listener()
    async def on_message(self, message):
      if message.channel.type == discord.ChannelType.private:
          return
      msg_content = message.content.lower()
      # finish rest of your code

根据itzFlubby的答案,这里有另一个解决方案,当您试图获取您的ID时,它可能不会让您进入可能的费率限制

查看以下代码:

async def on_消息(self,message):
msg_content=message.content.lower()
curseWord=['YourWords']
如果有(msg_内容中的单词对应于curseWord中的单词):
如果message.channel.type==discord.ChannelType.private:
返回#忽略DMs
等待消息。删除()
嵌入=discord.embed(title=“No Word”,
description=f“{message.author.ention},嘿!这里不允许使用这些词!”,
颜色=0x00FFFF)
author=message.author
pfp=author.avatar\u url
embed.set_author(name=f“{author}”,icon_url=pfp)
等待message.channel.send(嵌入=嵌入)
user=self.bot.get\u用户(YourID)
等待用户。发送(f“嘿,科尔!我刚才在**{message.guild.name}**中发现**{message.author}**在说**{msg\u content}**”。)#通过DM发送给你
在这里,我们不获取ID,只将其发送给您定义的
用户