You're getting this error because the bot is unable to send the DM because the user has their DMs disabled or friends only, You can list the users who didn't get the DMs.
Here is how you can see who didn't get the DMs.
@bot.command()
@commands.has_permissions(administrator=True)
async def dm(ctx, *, message=None):
await ctx.send(f"{ctx.author.mention} Message sent to all users in this server except the users listed below.")
await ctx.message.delete()
if message:
for guilds in bot.guilds:
members = guilds.members
for m in members:
try:
await m.send(message)
except discord.Forbidden: # discord.Forbidden means that the bot can't be sent.
await ctx.send(f"{m.name}#{m.discriminator}")
I hope this helped, I felt like Kelo's answer wasn't really explaining much so I tried to help and improve the code.
Have a nice day!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…