【问题标题】:Discord.js's `user.tag` and `user.username` in Discord.py?Discord.py 中的 Discord.js 的 `user.tag` 和 `user.username`?
【发布时间】:2021-04-11 04:29:09
【问题描述】:

我正在尝试用 Python 编写一个 Discord 机器人来获得乐趣,根据我为 discord.js (https://discord.js.org/#/docs/main/stable/class/User) 找到的文档,应该有一个 user.taguser.username 给你一个字符串(I'我正在为message.author(用户的子类)寻找一种方法)。这在discord.py 中不起作用,我在discord.py 中找不到类似的东西。有谁知道怎么做?

参考代码:

@client.event
async def on_message(message):
    print(message.content + " sent by " + message.author.username + message.author.tag)
    if message.author == client.user:
        return
    Reply = "Test"
    await message.channel.send(Reply)

【问题讨论】:

  • 请先阅读discord.py documentation,您可能会在不同的类名下找到相同的类。我的一个小建议是不要将discord.jsdiscord.py 视为相同。它们可能相似,尽管您可能能够找到相同的函数和/或数据,但它们在实际代码中却截然不同。

标签: python discord discord.js discord.py


【解决方案1】:

如果您正在寻找鉴别器 (9999),请使用 Member.discriminator

message.author.discriminator

或者,如果您正在寻找全名 (HelloWorld#9999)

message.author.name

你的源代码

@client.event
async def on_message(message):
    print(message.content + " sent by " + message.author.name)
    if message.author == client.user:
        return
    Reply = "Test"
    await message.channel.send(Reply)

参考:

【讨论】:

    【解决方案2】:

    没有Member.tag 这样的东西,我想你正在寻找Member.discriminator

    tag = message.author.discriminator
    

    或者,如果您正在寻找全名 (example#1234)

    name = str(message.author) # Or `message.author.name`
    

    参考:

    【讨论】:

      【解决方案3】:

      你可以这样做:

      name = message.author.name 
      

      然后拼接字符串得到标签,通过#拼接

      【讨论】:

        猜你喜欢
        • 2021-08-02
        • 2019-09-21
        • 2012-03-23
        • 1970-01-01
        • 2021-01-07
        • 2011-07-01
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多