簡體   English   中英

如何在 discord.py 中獲取服務器所有者名稱

[英]How can i get the server owner name in discord.py

我使用:await ctx.send(str(ctx.guild.owner.id)) 或 await ctx.send(ctx.guild.owner.id) 但機器人說“無”。 我怎樣才能解決這個問題

您需要獲取discord.Member object 而不是字符串 object。為此,您可以使用get_member function。這是一個簡單的 function 到 883437106334 所有者到頻道的所有者:

@client.command()
async def owner_find(ctx):
    guild_owner = client.get_user(int(ctx.guild.owner.id))
    await ctx.send(f'The owner of this server is: {guild_owner}')

您需要意圖才能獲取成員的信息。 由於ctx.guild.ownerdiscord.Member object。您需要成員意圖。

import discord
from discord.ext import commands

intents= discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix="!", intents=intents)

您還需要從 discord 開發人員門戶打開意圖。

參考(關於如何啟用意圖的指南)

PS:在 DM 中運行命令也會返回None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM