Reputation: 101
I understand the question is not phrased correctly or may not make any sense, so I will give a little context and information as to what I am trying to do...
Context
I am trying to create a bot that DMs everyone in my server to remind them of such and such, I have mastered the code on how to do this but I am having troubles figuring out how to make it show, who the bot is DMing and who it is not DMing. I have researched for quite a bit and haven't found a solution which lead me here.
Question How can I show who my bot is DMing and cannot DM. (The bot does do what it is supposed to do, DM everyone in the server upon request but I want it to be shown via terminal/pycharm/IDE.
Ex: User#1000 has successfully messaged User#2000!
import discord
import os, time, random
from discord.ext import commands
from lol import token
client = discord.Client()
intents = discord.Intents.all()
client = commands.Bot(command_prefix="!", intents=intents, self_bot = True)
@client.event
async def on_ready():
print("Ready!")
@client.command()
async def dm_all(ctx, *, args=None):
if args != None:
members = ctx.guild.members
for member in members:
try:
await member.send(args)
await print(ctx.discriminator.author)
except:
print("Unable to DM user because the user has DMs off or this is a bot account!")
else:
await ctx.send("Please provide a valid message.")
client.run(token, bot=True)
Upvotes: 0
Views: 449
Reputation: 3592
Here are some important things to know:
If a user cannot receive a DM, then you get an Forbidden
error.
You can use except
statements to log these errors and display them in the console.
Most of the time you can't send direct messages to bots, then you get an HTTPException
error.
Have a look at the following code:
@client.command()
async def dm_all(ctx, *, args=None):
if args is not None:
members = ctx.guild.members
for member in members:
try:
await member.send(args)
print(f"Sent a DM to {member.name}")
except discord.errors.Forbidden:
print(f"Could not send a DM to: {member.name}")
except discord.errors.HTTPException:
print(f"Could not send a DM to: {member.name}")
else:
await ctx.send("Please provide a valid message.")
Output:
Sent a DM to Dominik
Could not send a DM to: BotNameHere
member.name
according to your wishes.Reference: https://discordpy.readthedocs.io/en/latest/api.html?highlight=forbidden#discord.Forbidden
Upvotes: 1