[英]How can I show who my bot is DMing and cannot DM. (discord.py)
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.我正在尝试创建一个机器人,它可以向我服务器中的每个人发送消息以提醒他们某事,我已经掌握了如何执行此操作的代码,但我无法弄清楚如何让它显示,该机器人是谁在 DMing 和谁不是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.问题我如何显示我的机器人正在 DMing 并且不能 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. (机器人确实做了它应该做的事情,根据请求向服务器中的每个人发送消息,但我希望它通过终端/pycharm/IDE 显示。
Ex: User#1000 has successfully messaged User#2000!例如:用户#1000 已成功向用户#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)
Here are some important things to know:以下是一些需要了解的重要事项:
If a user cannot receive a DM, then you get an Forbidden
error.如果用户无法收到 DM,则会收到Forbidden
错误。
You can use except
statements to log these errors and display them in the console.您可以使用except
语句来记录这些错误并在控制台中显示它们。
Most of the time you can't send direct messages to bots, then you get an HTTPException
error.大多数情况下,您无法向机器人发送直接消息,然后您会收到HTTPException
错误。
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: Output:
Sent a DM to Dominik
Could not send a DM to: BotNameHere
member.name
according to your wishes.当然,您可以根据自己的意愿自定义member.name
。Reference: https://discordpy.readthedocs.io/en/latest/api.html?highlight=forbidden#discord.Forbidden参考: https://discordpy.readthedocs.io/en/latest/api.html?highlight=forbidden#discord.Forbidden
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.