python - 如何使用discord py连续编辑你的昵称

标签 python python-requests discord.py

我正在设置不和谐机器人的最后一个功能,但在使用 task.loop 更新机器人昵称时遇到问题

我尝试了各种方法,包括与人交谈和阅读文档,我遇到的问题是你只能通过任务循环将 self 作为参数传递。我确信这是一个比我做的更容易的修复。

def get_current_price():
    threading.Timer(15.0, get_current_price).start()
    r = requests.get('https://www.bitmex.com/api/v1/instrument?symbol=XBT&columns=lastPrice&count=1&reverse=true')
    price1 = r.json()
    global price2
    price2 = str(price1[0]['lastPrice'])
@tasks.loop(seconds=15.0)
async def change_nick():

代码的顶部部分使用请求来捕获来自 API 的信息,然后从我发送的参数中选择我想要的特定数字。底部只是一些关于循环的正确代码,并显示了我的计时器

我尝试使用 guild.me self.edit 和文档中找到的其他内容,但我不确定当时是否实现,并且找不到任何示例来进行逆向工程和学习。

最佳答案

为了在discord.py中更改昵称,您必须首先获取一个Member对象,不要与User对象混淆。 Member 对象指的是 Guild 中的某人,而 User 对象只是整个平台上存在的某人,您只能更改 Member 对象昵称,因为它在公会内部引用它们。

你可以通过这样做轻松做到这一点

from discord.ext.commands import MemberConverter

guild = self.bot.get_guild(yourGuildIDHere) # Get the guild in question so you can actually get a member object

converter = MemberConverter()
member = await converter.convert(guild, yourid) # convert a userid to a member object reffering to the guild.

await member.edit(nick=nicknameHere)

关于python - 如何使用discord py连续编辑你的昵称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58229981/

相关文章:

python - 从链接数组中抓取 HTML

python - 如何在 Python 中为 Ant 媒体 API 发送发布请求

python - 如果达到 Wait_For_Message 超时则发送消息 Discord Py

python - Django - 在图像编辑表单中删除 "Currently"并将其替换为实际图像

python - 如果值是字符串列表,如何引用字典中的值?

python - 为什么Python请求HTTP库总是返回发送的cookie?

Python Bot 使用自定义表情符号

python - Discord 丰富的嵌入按钮

python - 使用线程或 asycore 在 python 中群聊应用程序

python - 将 y 轴自动缩放为 pyqtgraph 中的可见数据