【问题标题】:Discord.py how do I send a DM to anyone I want through a commandDiscord.py 如何通过命令向我想要的任何人发送 DM
【发布时间】:2019-12-11 22:24:55
【问题描述】:

有人让我为他制作一个机器人,通过命令(例如*send_dm @Jess#6461 hello)向他指定的任何人发送 DM。

我搜索了很多,我发现了这段代码:

async def send_dm(ctx,member:discord.Member,*,content):
    await client.send_message(member,content)

但后来我得到了错误:

discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:“Bot”对象没有属性“send_message”

我想输入例如:*send_dm @Jess#6461 hello,然后机器人会向该用户发送一个说“你好”的 DM。

【问题讨论】:

    标签: python discord


    【解决方案1】:

    discord.py 版本 1 中的client.send_message() has been replaced by channel.send()

    您可以使用Member.create_dm()创建一个向用户发送消息的通道

    async def send_dm(ctx, member: discord.Member, *, content):
        channel = await member.create_dm()
        await channel.send(content)
    

    【讨论】:

    • 然后我收到此错误:discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'channel' is not defined
    • 我是否也可以放置任何我想要的用户,即使它不在服务器中?
    • 可能,尝试使用User
    • 然后当我执行*send_dm <@330368617084616714> hello 时,它给了我一个用户不存在的错误:discord.ext.commands.errors.BadArgument: User "<@330368617084616714>" not found
    【解决方案2】:

    您好,希望对您有所帮助

    client.command(aliases=['dm'])
    async def DM(ctx, user : discord.User, *, msg):
        try:
            await user.send(msg)
            await ctx.send(f':white_check_mark: Your Message has been sent')
        except:
            await ctx.send(':x: Member had their dm close, message not sent')
    

    如果您使用任何其他 str,请更改您的客户端

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2021-01-29
      • 2022-01-14
      • 2018-07-11
      • 2020-07-28
      • 2021-05-17
      • 2021-12-03
      相关资源
      最近更新 更多