Discord bots can’t send DM’s to eachother, only to Users
. Your bot is trying to send a DM to itself, which it can’t. You need to check with an if-statement if the message’s author is a bot or not.
You’re already doing this later on in your code, to see if it should send the message or not, but the create_dm()
function at the top will have already failed at that point (as it can’t create a dm), so it will never get there.
@commands.Cog.listener()
async def on_message(self, message):
if message.author.bot:
return # Ignore bot messages
if not message.guild:
return
if not message.channel.name == 'roles':
return
elif message.content != 'role':
await message.author.send(message.content)
await message.delete()
PS. A few more things to note here:
You can just use
User.send()
instead ofcreate_dm()
&dm.send()
.An
f-string
with nothing but the content can just be the content itself (f"{word}"
==word
, orstr(word)
in case it’s not a string).You have an
if
checking if the channel name is not “roles”, and afterwards in theelif
you check if it is – if it wouldn’t be “roles” then it would’ve gotten into the firstif
& returned so the second one is unnecessary.
CLICK HERE to find out more related problems solutions.