【问题标题】:DM to a person by user id in discord.py通过 discord.py 中的用户 ID 向某人发送消息
【发布时间】:2021-04-12 19:15:16
【问题描述】:

我检查了很多答案,但这些答案使用ctx.author.send('something')。但我想通过用户 ID 发送,而不是 AUTHOR。

这是我的代码:

@bot.command()
async def dm(ctx):
    await ctx.send(496021236988510209, 'test')

而且它甚至不返回错误消息。

【问题讨论】:

标签: python discord discord.py-rewrite


【解决方案1】:

要获得服务器成员,请使用Guild.get_member

还有 Client.fetch_user 仅适用于机器人。

如果它应该可以工作但没有成员出现,那很可能是因为您在创建 discord 客户端之前忘记说明使用 members 的意图。例如:

import discord
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)

可能重复,here

【讨论】:

    【解决方案2】:

    您可以使用以下代码:

    @client.command()
    @commands.has_permissions(manage_roles = True)
    async def dm(ctx, users: Greedy[User], *, message):
        for user in users:
          await user.send(message)
    

    你只需做 dm @3422353423342 消息

    @3422353423342 代表一个用户id

    【讨论】:

    • 高质量的答案通常包括解释你的答案的基本部分如何/为什么解决了 OP 的问题。 edit 可能会澄清未来访问者的用途,以便他们获得一些东西来解决他们自己的编码问题。这就是随着时间的推移获得最多赞成票的方式。通常不鼓励仅使用代码回答。您可以编辑和/或记住以后的帖子。
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 2022-07-10
    • 1970-01-01
    • 2022-06-11
    • 2019-06-02
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多