首页 > 解决方案 > discord.py - “member” 参数总是返回一个字符串,即使使用转换器 discord.User

问题描述

正如标题中所说,我在 Moderation cog 中有一个静音函数,它接受一个成员参数,但是即使使用转换器,“member”最终还是一个看起来像“user#0000”的字符串。Moderation 类的所有其他方法都将成员作为对象。是什么原因造成的?

这是静音():

@commands.command()
@commands.guild_only()
@commands.has_permissions(kick_members=True)
async def mute(self, ctx, member: discord.Member, *, reason="unspecified"):
    """"""
    try:
        await member.add_roles(self.get_role(ctx.guild.id, "muted_role"))
        await member.remove_roles(self.get_role(ctx.guild.id, "default_role"))

    except Exception as e:
        await ctx.send(f"**Error**: `{e}`")

    else:
        await ctx.send(f"{member.id} was muted.")

        if not self.get_channel(ctx.guild.id, "log_channel"):
            await ctx.send(self.no_log_chan)
            return

        await self.get_channel(ctx.guild.id, "log_channel").send(
            self.get_embed(
                "__MUTE__",
                ctx.message.author,
                "#00FF00",
                f"{member.id} was muted by {ctx.message.author} \nReason : *{reason}*",
                datetime.now().strftime("%H:%M:%S"),
                ctx.message.author.avatar_url,
            )
        )

这是我的 kick() 方法,效果很好:

@commands.command()
@commands.guild_only()
@commands.has_permissions(kick_members=True)
async def kick(self, ctx, member: discord.User, *, reason="unspecified"):
    """"""
    try:
        await ctx.guild.kick(member)

    except Exception as e:
        await ctx.send(f"**Error**: `{e}`")

    else:
        await ctx.send(f"{member.id} was kicked.")

        if not self.get_channel(ctx.guild.id, "log_channel"):
            await ctx.send(self.no_log_chan)
            return

        await self.get_channel(ctx.guild.id, "log_channel").send(
            self.get_embed(
                "__KICK__",
                ctx.message.author,
                "#00FF00",
                f"{member.id} was kicked from the server by {ctx.message.author} \nReason : *{reason}*",
                datetime.now().strftime("%H:%M:%S"),
                member.avatar_url,
            )
        )

标签: pythondiscord.py

解决方案


直到最后一个版本,它的工作方式是这样的:

async def Function(ctx, member:discord.User):

希望它有用。


推荐阅读