Ask coding questions

← Back to all posts
How do I close a discord bots dms
h
DrSovietSoviet (0)

Recently my bot has been used by people it's not supposed to be used by via dms how would I disable it's dms as a quick work around?

Answered by InvisibleOne (3186) [earned 5 cycles]
View Answer
Comments
hotnewtop
InvisibleOne (3186)

If you are using commands you can use

@bot.command(no_pm=True)

or you can check if there is a server on the message

@bot.event
async def on_message(message):
  if not message.server:
   return
xfinnbar (134)

Override the default on_message event and check if the channel is a DM before processing commands. If you are using bot instead of client for discordpy, then change client to bot:

@client.event # or bot if there is error
async def on_message(message):
if not isinstance(message.channel, discord.TextChannel):
  client.process_commands(message)