Pycord 静音命令不会抛出错误但不会添加角色

Pycord mute command doesn't throw error but doesn't add role

我为此使用了 pycord,因此我可以使用斜杠命令。

这是我的代码:

import discord, os
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv(f'{os.getcwd()}/token.env')

bot = commands.Bot()

token = str(os.getenv('TOKEN'))
@bot.slash_command(description = 'mutes member of choice')
@commands.has_permissions(administrator = True)
async def mute(ctx, member: discord.Member):
    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted")

    if not mutedRole:
        mutedRole = await guild.create_role(name="Muted")

        for channel in guild.channels:
            await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True, read_messages=False)

    await member.add_roles(mutedRole)
    await ctx.send(f"Muted {member.mention}")

@mute.error
async def permserror(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        ctx.respond('Sorry! You dont have permission.')

@bot.event
async def on_ready():
    print(f'Client ready as {bot.user}')

bot.run(token)

这不是我的完整代码,但其他命令对我的问题来说似乎不是必需的。

无论如何,当我尝试使用这个命令时,它没有在控制台中抛出错误,但应用程序没有响应,也没有添加角色。

我在每行代码之间放了一个打印语句,它似乎就在添加角色之前停止了。

我该如何解决这个问题?

更新:我以前的代码没有运行所以我更新了它。

这实际上与程序代码无关。您所要做的就是将机器人角色移到顶部,它就会起作用。错误是 403 Forbidden (error code: 50013): Missing Permissions。此外,您必须重新邀请机器人并添加 applications.commands(如果您还没有)。