我的代码如下:
#Mute Command
@bot.command(pass_context = True)
async def mute(ctx, member: discord.Member):
if ctx.message.author.server_permissions.administrator or ctx.message.author.id == '585578670207401984':
role = discord.utils.get(member.server.roles, name='Muted')
await ctx.add_roles(member, role)
await ctx.send("User {0} was muted by {1}!")
else:
await ctx.send("You do not have permission to mute this member.")
但是,当我使用命令时,出现此错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Member' object has no attribute 'server_permissions'
有人可以帮忙吗?
答案 0 :(得分:0)
您在代码中有几个错误,属性错误。
Member
对象没有属性server_permissions
,但是具有guild_permissions
。
您无法将成员ID与字符串进行比较,而需要与整数进行比较。
Member
对象没有属性server
。您需要将member.server.roles
更改为member.guild.roles
。
您无法使用ctx.add_roles
添加角色,需要执行member.add_roles
。
在第await ctx.send("User {0} was muted by {1}!")
行,您没有设置变量的格式,因此它将User {0} was muted by {1}!
发送到聊天室。您可以执行await ctx.send("User {0} was muted by {1}!".format(member.mention, message.author.mention))
对其进行格式化。
您可以将代码更改为:
@bot.command(pass_context = True)
async def mute(ctx, member: discord.Member):
if ctx.message.author.guild_permissions.administrator or ctx.message.author.id == 585578670207401984:
role = discord.utils.get(ctx.guild.roles, name='Muted')
await member.add_roles(role)
await ctx.send("User {0} was muted by {1}!".format(member.mention, message.author.mention))
else:
await ctx.send("You do not have permission to mute this member.")