我正在用 Python 制作一个 Discord SelfBot 并收到一个静音命令的错误

时间:2021-04-26 04:26:20

标签: python discord

我正在用 Python 制作一个 Discord SelfBot,我收到了一个让我感到困惑的静音命令的错误。

这是静音命令的代码

@commands.command(name="mute", description="Mute a user from the command server.", usage=" [@member] (reason)")
    async def mute(self, ctx, member: discord.Member, *, reason="Undefined"):
        if ctx.author.guild_permissions.mute_members:
            mutedRole = get(ctx.guild.roles, name="Muted")
            await member.add_roles(mutedRole)

这是错误所在,我似乎无法修复它

mutedRole = get(ctx.guild.roles, name="Muted")

这是当我将鼠标悬停在错误部分时显示的内容

[pyflakes] undefined name 'get'

当我拿走“get”时,它给了我一个错误

name="Muted")

[pyflakes] invalid syntax

Error With 'get'

Error without 'get'

1 个答案:

答案 0 :(得分:0)

根据 Ceres 的评论,这里有一个可能的答案:

您想使用 get 但您似乎从未“导入”过它。要克服您遇到的错误,您必须导入以下内容:

from discord.utils import get

我还注意到您正在使用reason,但不要将其插入到您的代码中。

一个可能的完整代码是:

from discord.utils import get # Import get to get the role

@commands.command(name="mute", description="Mute a user from the command server.", usage=" [@member] (reason)")
    async def mute(self, ctx, member: discord.Member, *, reason="Undefined"):
        if ctx.author.guild_permissions.mute_members:
            mutedRole = get(ctx.guild.roles, name="Muted")
            await member.add_roles(mutedRole, reason=reason) # Add reason

有关详细信息,请查看 docs