'(Discord.py) Make my Bot only Respond to DM's
So I managed to get the basics of my Bot working which is good. However, when I try and send a message, for example execute /ping
, the bot replies to it regardless of what Channel the command is executed in, including DMs! I then programmed a statement which made it only respond to commands in certain Discord Channels on the server, and this does appear to work. However, when I try to change the channels array to discord.DMChannel
, this does not appear to work. Could someone help me fix this?
channels = ["general"]
if str(message.channel) in channels:
if message.content == "/ping":
await message.channel.send("Pong")
But if I change it to this:
channels = [discord.DMChannel]
if str(message.channel) in channels:
if message.content == "/ping":
await message.channel.send("Pong")
The bot does not respond to DMChannel messages.
Solution 1:[1]
You could try setting bot intents, but keep in mind these affect the entire bot, not just one command
...
my_intents = discord.Intents.default()
my_intents.guild_messages = False # turn off messages from guilds, so you only get messages from DM channels
# optionally turn on members or presences here if you need them
client = discord.Client(..., intents=my_intents) # or discord.Bot() or whatever
...
Solution 2:[2]
You can simply do
await ctx.author.send
It will only send the respond in dms. It can also be used in channels but it will send the respond in dms.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eric Jin |
Solution 2 | cigien |