【问题标题】:I need help making a discord py temp mute command in discord py我需要帮助在 discord py 中创建 discord py temp 静音命令
【发布时间】:2020-07-09 05:00:20
【问题描述】:

我让我的不和谐机器人有一个静音命令,但您必须稍后自己取消静音用户,我想要另一个名为“tempmute”的命令将成员静音一定分钟/小时/或天,这是我到目前为止的代码,我将如何从中发出临时静音命令?

#mute command 
@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member=None):
    if not member:
        await ctx.send("Who do you want me to mute?")
        return
    role = discord.utils.get(ctx.guild.roles, name="muted")
    await member.add_roles(role)
    await ctx.send("ok I did it")

【问题讨论】:

  • 我对 python 不是很好,但通常你会想用日期来做这件事。您可以将日期存储在数据库或(不推荐)变量中,然后定期检查该日期是否已过,然后在该日期过后取消静音。

标签: python discord discord.py


【解决方案1】:

类似于您如何赋予他们静音的角色,只需添加另一个参数以获取您希望他们静音的时间(以秒为单位)。然后,您可以在删除该角色之前使用 await asyncio.sleep(mute_time)。

代码如下所示:

import asyncio

#mute command 
@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: discord.Member=None, mute_time : int):
    if not member:
        await ctx.send("Who do you want me to mute?")
        return
    role = discord.utils.get(ctx.guild.roles, name="muted")
    await member.add_roles(role)
    await ctx.send("ok I did it")

    await asyncio.sleep(mute_time)
    await member.remove_roles(role)
    await ctx.send("ok times up")

【讨论】:

  • 如果我需要做 1h、1d 或 1w 怎么办?
【解决方案2】:
import asyncio

@commands.command()
@commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.Member,time):
    muted_role=discord.utils.get(ctx.guild.roles, name="Muted")
    time_convert = {"s":1, "m":60, "h":3600,"d":86400}
    tempmute= int(time[0]) * time_convert[time[-1]]
    await ctx.message.delete()
    await member.add_roles(muted_role)
    embed = discord.Embed(description= f"✅ **{member.display_name}#{member.discriminator} muted successfuly**", color=discord.Color.green())
    await ctx.send(embed=embed, delete_after=5)
    await asyncio.sleep(tempmute)
    await member.remove_roles(muted_role)

【讨论】:

  • 欢迎。尝试在您的答案中添加一些解释,以提高质量和一般理解。
  • 我可以知道time convert 是什么吗?
  • 我认为这不是很好的解决方案。如果机器人崩溃或关闭,角色将永远存在。
猜你喜欢
  • 1970-01-01
  • 2021-08-12
  • 2021-05-31
  • 2021-02-21
  • 2021-10-09
  • 2021-06-28
  • 2021-05-22
  • 1970-01-01
  • 2020-12-23
相关资源
最近更新 更多