Reputation: 74
I get the error, await channel.send(f"Hey Cole! I caught **{message.author}** saying **{msg_content}** in **{message.guild.name}** just now.") AttributeError: 'NoneType' object has no attribute 'name'
Whenever i type a bad word, i get the error above. It still dm's me though but it says that the bot said the bad word. How can i fix this? Thanks
my code is:
@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))
Upvotes: 0
Views: 215
Reputation: 3602
Based on the answer of itzFlubby here is another solution which might not get you into a possible ratelimit as you are trying to fetch your ID.
Have a look at the following code:
async def on_message(self, message):
msg_content = message.content.lower()
curseWord = ['YourWords']
if any(word in msg_content for word in curseWord):
if message.channel.type == discord.ChannelType.private:
return # Ignore DMs
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 = self.bot.get_user(YourID)
await user.send(f"Hey Cole! I caught **{message.author}** saying **{msg_content}** in **{message.guild.name}** just now.") # Send it to you via DM
Here we do not fetch the ID and just send it to your defined user
.
Upvotes: 2
Reputation: 2289
You've run into an logic problem!
Since you're code works the fist time, you get the DM. The message your bot sends to your DM also triggers a new on_message
event. Since the bot repeats the forbidden words in your DM, your bot gets caught in its own spamfilter, and wants to send you another DM about this. Btw, this would result in an infinite loop, if the attribute error didn't occur.
And this also explains, why it occurs:
The second time your on_message
event runs it was triggered in your DM by your own bot, so message.guild
will be None
. To overcome this, you can for example ignore messages in DM-Chats.
@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
Upvotes: 4