Reputation: 13
I have this code:
@client.event
async def on_message(message):
print(message.author)
print(message.content)
this code prints a feed of all of the messages send from channels that the bot has permissions to see. basically i'm trying to write code that does the same with dms. and I can't figure out how.
Upvotes: 1
Views: 274
Reputation: 121
You can also make the bot send DMs to a specific channel using the following -
Just make sure to replace 1234567890
with channel id you want the bot to send the DMs to.
@client.event
async def on_message(message):
if isinstance(message.channel, discord.DMChannel):
emb=discord.Embed(title=message.author, description=message.content)
channel = client.get_channel(1234567890)
await channel.send(embed=emb)```
Upvotes: 0
Reputation: 528
As Stijndcl said above, you want to check if the message is sent in a guild, but you also want to check if the bot sends the message.
if message.author == client.user:
return
if not message.guild:
print(f"DM: {message.content}")
If you code your bot further you might end up DM'ing users, and you don't want to log your own messages.
Upvotes: 0
Reputation: 1321
You could use:
if isinstance(message.channel, discord.DMChannel):
print(message.author)
print(message.content)
Upvotes: 0
Reputation: 5650
To check if a message is a dm, check if the Guild
is None
.
if message.guild is None:
print(f"DM: {message.content}")
Upvotes: 1