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.
To check if a message is a dm, check if the Guild
is None
.
if message.guild is None:
print(f"DM: {message.content}")
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.
You could use:
if isinstance(message.channel, discord.DMChannel):
print(message.author)
print(message.content)
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)```
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.