【问题标题】:Discordpy TempMute CommandDiscordpy TempMute 命令
【发布时间】:2020-11-21 05:52:37
【问题描述】:

对 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")
    ```

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-19
      • 2021-10-14
      • 1970-01-01
      • 2021-05-29
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多