【问题标题】:Tempmute command in discord.py its not removing the muterolediscord.py 中的 Tempmute 命令不会删除 muterole
【发布时间】:2021-09-29 23:57:15
【问题描述】:
@client.command(description="Mutes the specified user.")
@commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member, time=None, *, reason=None):
    time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
    mute_time = int(time[0]) * time_conversion[time[-1]]
    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted by ez")
    await member.add_roles(mutedRole, reason=reason)
    mute = discord.Embed(
        description=f"<a:mutediscord:888827254853996564> {member.mention} was muted.\n**Reason:** {reason}\n**Duration** {time}",
        color=ctx.author.color)
    mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    muteprvt = discord.Embed(
        description=f"<a:mutediscord:888827254853996564> You have muted from {ctx.guild.name}\n**Reason:** {reason}\n**Duration** {time}",
        color=ctx.author.color)
    muteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    await ctx.channel.send(embed=mute)
    await ctx.member.send(embed=muteprvt)
    await asyncio_sleep(mute_time)
    await member.remove_roles(mutedRole)
    unmute = discord.Embed(description=f"???? {member.mention} was unmuted.", color=ctx.author.color)
    unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    await ctx.channel.send(embed=unmute)
    unmuteprvt = discord.Embed(description=f"???? You have unmuted from {ctx.guild.name}", color=ctx.author.color)
    unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    await member.send(embed=unmuteprvt)
    return

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

        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, reason=reason)
    mute = discord.Embed(
        description=f"<a:mutediscord:888827254853996564> {member.mention} was muted.\n**Reason:** {reason}\n**Duration** {time}",
        color=ctx.author.color)
    mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    muteprvt = discord.Embed(
        description=f"<a:mutediscord:888827254853996564> You have muted from {ctx.guild.name}\n**Reason:** {reason}\n**Duration** {time}",
        color=ctx.author.color)
    muteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    await ctx.channel.send(embed=mute)
    await ctx.member.send(embed=muteprvt)
    await asyncio_sleep(mute_time)
    await member.remove_roles(mutedRole)
    unmute = discord.Embed(description=f"???? {member.mention} was unmuted.", color=ctx.author.color)
    unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    await ctx.channel.send(embed=unmute)
    unmuteprvt = discord.Embed(description=f"???? You have unmuted from {ctx.guild.name}", color=ctx.author.color)
    unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
    await member.send(embed=unmuteprvt)
    return

这就是我的代码问题是它没有删除角色有人可以帮助我吗? 我已经转换了时间和东西,所以它一切正常,但 idk 可能 asyncio 模块不工作 bcs 也没有审计日志中的任何内容

【问题讨论】:

  • 尝试使用print 语句来查看机器人卡在哪里。这是很多代码,而且很难找出错误所在。
  • 如何使用它是新的,从未使用过的打印命令
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python discord discord.py


【解决方案1】:

错误是由于这里的各种原因造成的。

你不使用asyncio 你在那里写的方式。请改用asyncio.sleep(mute_time) ,因为asyncio_sleep 不存在。

此外,您应该知道无法访问return 后面的代码,因为之后的所有内容都被阻止,如我的屏幕截图所示。代码如何仍然为您工作是值得怀疑的。

截图:

此外,您应该提前检查mutedRole 是否存在,否则您将收到AttributeError 错误。因此,只需将您的 if not mutedRole: 声明放在您尝试添加角色之前的某处。

【讨论】:

  • 请注意我的编辑
  • 我认为这是我的时间转换
  • 必须是mute_time = int(time[:-1]) * time_conversion[time[-1]]asyncio.sleep(mute_time)
  • 是的,我试过了,现在这也不起作用:|我添加了你的功能......
【解决方案2】:

@client.command(description="Mutes the specified user.")
@commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member, time=None, *, reason=None):
    time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
    mute_time = int(time[:-1]) * time_conversion[time[-1]]
    print(mute_time)
    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted by ez")
    if not mutedRole:
        mutedRole = await guild.create_role(name="Muted by ez")

        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, reason=reason)
        mute = discord.Embed(
            description=f"<a:mutediscord:888827254853996564> {member.mention} was muted.\n**Reason:** {reason}\n**Duration** {time}",
            color=ctx.author.color)
        mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        muteprvt = discord.Embed(
            description=f"<a:mutediscord:888827254853996564> You have muted from {ctx.guild.name}\n**Reason:** {reason}\n**Duration** {time}",
            color=ctx.author.color)
        muteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        await ctx.channel.send(embed=mute)
        await ctx.member.send(embed=muteprvt)
        await asyncio.sleep(mute_time)
        await member.remove_roles(mutedRole)
        unmute = discord.Embed(description=f"? {member.mention} was unmuted.", color=ctx.author.color)
        unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        await ctx.channel.send(embed=unmute)
        unmuteprvt = discord.Embed(description=f"? You have unmuted from {ctx.guild.name}", color=ctx.author.color)
        unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        await member.send(embed=unmuteprvt)
    else:
        await member.add_roles(mutedRole, reason=reason)
        mute = discord.Embed(
            description=f"<a:mutediscord:888827254853996564> {member.mention} was muted.\n**Reason:** {reason}\n**Duration** {time}",
            color=ctx.author.color)
        mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        muteprvt = discord.Embed(
            description=f"<a:mutediscord:888827254853996564> You have muted from {ctx.guild.name}\n**Reason:** {reason}\n**Duration** {time}",
            color=ctx.author.color)
        muteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        await ctx.channel.send(embed=mute)
        await ctx.member.send(embed=muteprvt)
        await asyncio.sleep(mute_time)
        await member.remove_roles(mutedRole)
        unmute = discord.Embed(description=f"? {member.mention} was unmuted.", color=ctx.author.color)
        unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        await ctx.channel.send(embed=unmute)
        unmuteprvt = discord.Embed(description=f"? You have unmuted from {ctx.guild.name}", color=ctx.author.color)
        unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
        await member.send(embed=unmuteprvt)

添加了你的所有功能

【讨论】:

    【解决方案3】:

    @client.command(description="Mutes the specified user.")
    @commands.has_permissions(manage_messages=True)
    async def mute(ctx, member: discord.Member, time=None, *, reason=None):
        time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400}
        mute_time = int(time[:-1]) * time_conversion[time[-1]]
        print(mute_time)
        guild = ctx.guild
        mutedRole = discord.utils.get(guild.roles, name="Muted by ez")
        if not mutedRole:
            mutedRole = await guild.create_role(name="Muted by ez")
    
            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, reason=reason)
            mute = discord.Embed(
                description=f"<a:mutediscord:888827254853996564>   {member.mention} was muted.\n**Reason:** {reason}\n**Duration** {time}",
                color=ctx.author.color)
            mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await ctx.channel.send(embed=mute)
            mutep = discord.Embed(
                description=f"<a:mutediscord:888827254853996564>   You have muted from {ctx.guild.name}\n**Reason:** {reason}\n**Duration** {time}",
                color=ctx.author.color)
            mutep.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await member.send(embed=mutep)
            await asyncio.sleep(mute_time)
            await member.remove_roles(mutedRole)
            unmute = discord.Embed(description=f"? {member.mention} was unmuted.", color=ctx.author.color)
            unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await ctx.channel.send(embed=unmute)
            unmuteprvt = discord.Embed(description=f"? You have unmuted from {ctx.guild.name}", color=ctx.author.color)
            unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await member.send(embed=unmuteprvt)
        else:
            await member.add_roles(mutedRole, reason=reason)
            mute = discord.Embed(description=f"<a:mutediscord:888827254853996564>   {member.mention} was muted.\n**Reason:** {reason}\n**Duration** {time}", color=ctx.author.color)
            mute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await ctx.channel.send(embed=mute)
            mutep = discord.Embed(
                description=f"<a:mutediscord:888827254853996564>   You have muted from {ctx.guild.name}\n**Reason:** {reason}\n**Duration** {time}",
                color=ctx.author.color)
            mutep.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await member.send(embed=mutep)
            await asyncio.sleep(mute_time)
            await member.remove_roles(mutedRole)
            unmute = discord.Embed(description=f"? {member.mention} was unmuted.", color=ctx.author.color)
            unmute.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await ctx.channel.send(embed=unmute)
            unmuteprvt = discord.Embed(description=f"? You have unmuted from {ctx.guild.name}", color=ctx.author.color)
            unmuteprvt.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)
            await member.send(embed=unmuteprvt)
    

    得到它修复它实际上只是嵌入是错误的哈哈。 所以谁想用它你就可以拥有它 感谢您对 Dominik 的帮助!

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 1970-01-01
      • 2021-02-09
      • 2020-11-21
      • 2019-03-24
      • 2021-09-26
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      相关资源
      最近更新 更多