Discordpy审核命令的新增功能,并想知道为什么此代码不起作用, 因为尝试运行它时没有错误。应该根据用户单位将用户暂时静音一段时间,如果没有创建静音角色,则会创建一个。
@commands.command()
async def mute(self , ctx, user : discord.Member, duration = 0,*, unit = None):
guild = ctx.guild
for role in guild.roles:
if role.name =="Muted":
await member.add_roles(role)
await ctx.send("{} Has been muted by {} for {duration}{unit} " .format(member.mention,ctx.author.mention))
return
overwrite = discord.PermissionsOverwrite(send_messages=False)
newRole = await guild.create_role(name="Muted")
for channel in guild.text_channels:
await channel.set_permissions(newRole,overwrite=overwrite)
await member.add_roles(newRole)
await ctx.send("{} Has been muted by {} for {duration}{unit} " .format(member.mention,ctx.author.mention))
if unit == "s":
wait = 1 * duration
await asyncio.sleep(wait)
elif unit == "m":
wait = 60 * duration
await asyncio.sleep(wait)
await user.remove_roles(roleobject)
await ctx.send(f":white_check_mark: {user} was unmuted")
```
答案 0 :(得分:0)
return语句之后的所有代码都是无用的,因为它将永远不会执行。也许您将这一部分缩进得太远了。 列出角色名称并进行检查,而不是大循环
rolenames = {role.name: role for role in guild.roles}
if 'Muted' not in rolenames.keys():
# Make the new role
role = new_role_you_created
else:
role = rolenames['Muted']
# Add the role to the user
await member.add_roles(role)