[英]discord.py mute command not adding roles
async def mute(ctx, user: discord.User,time,*,reason):
if user:
await ctx.message.delete()
if discord.utils.get(ctx.guild.roles, name="mute"):
role = discord.utils.get(ctx.guild.roles, name="mute")
addroles = []
for i in user.roles:
try:
await user.remove_roles(i)
addroles.append(i.id)
except:
print(f"Can't remove the role {i}")
await user.add_roles(role)
cmd_used("mute",ctx.author,ctx.message.content)
embed=discord.Embed(title=f"""{bot.user.name}""",colour=maincolor)
embed.add_field(name=f"""`{ctx.author}` muted you from `{user.guild.name}`""",value=f"""time: {time}s, reason: {reason}""",inline=True)
await user.send(embed=embed)
await asyncio.sleep(int(time))
await user.remove_roles(role)
print(addroles)
await user.add_roles(addroles)
#errors: AttributeError: 'list' object has no attribute 'id'
基本上我的静音命令实际上不起作用,因为它使他们静音并且不会恢复他们的旧角色。 我需要一些帮助。
addroles
在您的情况下是用户在命令之前拥有的角色 ID 的列表。 但是add_roles(role)
将角色作为参数,而不是角色 ID 列表。
您可以将实际角色 object 保存在addroles
中,遍历该列表并分配它们
async def mute(ctx, user: discord.User,time,*,reason):
if user:
await ctx.message.delete()
if discord.utils.get(ctx.guild.roles, name="mute"):
role = discord.utils.get(ctx.guild.roles, name="mute")
addroles = []
for i in user.roles:
try:
await user.remove_roles(i)
addroles.append(i)
except:
print(f"Can't remove the role {i}")
await user.add_roles(role)
cmd_used("mute",ctx.author,ctx.message.content)
embed=discord.Embed(title=f"""{bot.user.name}""",colour=maincolor)
embed.add_field(name=f"""`{ctx.author}` muted you from `{user.guild.name}`""",value=f"""time: {time}s, reason: {reason}""",inline=True)
await user.send(embed=embed)
await asyncio.sleep(int(time))
await user.remove_roles(role)
print(addroles)
for role in addroles:
await user.add_roles(role)
或者更好的方法是使用discord.Member.edit()
,因为它只向 Discord API 发送一个请求。
例如,您可以使用以下命令删除用户的所有角色
await user.edit(roles = [])
如您所见,这效率更高,应该是首选解决方案
async def mute(ctx, user: discord.User,time,*,reason):
if user:
await ctx.message.delete()
if discord.utils.get(ctx.guild.roles, name="mute"):
role = discord.utils.get(ctx.guild.roles, name="mute")
addroles = user.roles
await user.edit(roles = [role]) # remove all roles and only give the user the mute-role
cmd_used("mute",ctx.author,ctx.message.content)
embed=discord.Embed(title=f"""{bot.user.name}""",colour=maincolor)
embed.add_field(name=f"""`{ctx.author}` muted you from `{user.guild.name}`""",value=f"""time: {time}s, reason: {reason}""",inline=True)
await user.send(embed=embed)
await asyncio.sleep(int(time))
await user.edit(roles = addroles) # gives old roles back, overwrites the mute role
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.