【问题标题】:Discord DMs using bot使用机器人的 Discord DM
【发布时间】:2021-05-11 22:26:25
【问题描述】:

所以今天尝试学习python discord.py。

在某些时候,我尝试了以下事情: 一个人告诉机器人一个命令($DM 'discord user' 'content'),通过我所做的所有研究,我只能找到client.author.send("message") where client = discord.Client()。

有什么办法可以做到:

user = 'example#0000'
client.user.send("message")

?

【问题讨论】:

  • 你必须添加你当前的代码,有很多方法可以做到这一点,如果你能提供一些代码sn-p,我会很有帮助的

标签: python discord.py


【解决方案1】:

有几种方法可以做到这一点

来自用户 ID

@client.command()
async def DM(ctx, id: int, *, content):
    user = client.get_user(id)
    await user.send(content)

# Invoking
# $DM 123123123123123 something here

来自用户名#discriminator

@client.command()
async def DM(ctx, username, *, content):
    name, discriminator = username.split("#")
    user = discord.utils.get(ctx.guild.members, name=name, discriminator=discriminator)
    await user.send(content)

# Invoking
# $DM example#1111 something here

带有用户提及、ID、姓名、昵称(最好的选择 imo)

@client.command()
async def DM(ctx, member: discord.Member, *, content):
    await member.send(content)

# Invoking
# $DM @example#1111 something here
# $DM 1231231231231 something here
# $DM example#1111 something here

还要确保您启用了intents.members,有关更多信息,请查看我的previous answers之一

PS:您需要使用 commands.Bot 才能使所有这些工作,而不是 discord.Client

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 2020-09-14
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2018-08-22
    • 1970-01-01
    • 2021-02-20
    相关资源
    最近更新 更多