如何在 discord.py 中获取服务器所有者名称

时间:2021-02-17 19:38:02

标签: python discord.py owner

我使用: await ctx.send(str(ctx.guild.owner.id)) 或 await ctx.send(ctx.guild.owner.id) 但机器人说“无”。我该如何解决这个问题

2 个答案:

答案 0 :(得分:0)

您需要获取一个 discord.Member 对象而不是字符串对象。为此,您可以使用 get_member 函数。这是一个将服务器所有者输出到频道的简单函数:

@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}')

答案 1 :(得分:0)

您需要意图才能获取会员信息。由于 ctx.guild.ownerdiscord.Member 对象。您需要会员意图。

import discord
from discord.ext import commands

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

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

您还需要从 Discord 开发者门户启用 Intent。

Reference (With guide on how to enable intents)

P.S:在 DM 中运行命令也会返回 None