简体   繁体   English

如何显示我的机器人是谁在私信,谁不能私信。 (discord.py)

[英]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:以下是一些需要了解的重要事项:

  1. If a user cannot receive a DM, then you get an Forbidden error.如果用户无法收到 DM,则会收到Forbidden错误。

  2. You can use except statements to log these errors and display them in the console.您可以使用except语句来记录这些错误并在控制台中显示它们。

  3. 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
  • Of course you can customize 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.

相关问题 discord.py | 我的机器人无法将 DM 发送给其他机器人 - discord.py | My bot cannot send DM to other bot Discord.py - 我的 discord.bot 如何在特定时间向某人发送消息? - 解决方案在这里 - Discord.py - how can my discord.bot dm message to someone at certain time? - Solution here Discord.py - 我怎样才能通过我的机器人获得语音频道中所有成员的数量? - Discord.py - How can I get the amount of all members who are in a voicechannel with my bot? 将 txt 文件的内容作为 dm 发送。 不和谐.py - sending the content of a txt file as a dm. discord.py 如何使用机器人对所有人进行DM - discord.py - How to DM everyone with a bot - discord.py 如何在 on_message 事件 discord.py 后触发我的机器人向用户发送 DM - How do I trigger my bot to DM a user after an on_message event discord.py 如何添加对我的 discord.py 机器人的反应? - How can I add reaction with my discord.py bot? 如何为我的 discord.py 机器人添加冷却时间? - How can I add cooldown to my discord.py bot? (Discord.py) 让我的机器人只响应 DM - (Discord.py) Make my Bot only Respond to DM's (discord.py) 如何让我的机器人读取发送给它的 DM 并打印它们或将它们发送到特定频道 - (discord.py) How do I get my bot to read DM's that are sent to it and either print them or send them to a specific channel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM