繁体   English   中英

似乎无法让静音命令与 Discord py 一起使用

[英]Can not seem to get mute commands to work with Discord py

我找不到我的不和谐机器人的静音命令,我不知道怎么写。 那里的教程和代码段不起作用。 最新的代码不会产生错误消息,但不会启动该命令。 如何?

@commands.has_permissions(manage_messages=True)
async def mute(ctx, member: discord.member, *, reason=None):
  guild =ctx.guild
  mutedRole = discord.Utils.get(guild.roles, name="Muted")
  if not mutedRole == await guild.create_role(name="Muted"):
    for channel in guild.channels:
      await channel.set_permissions(mutedRole, speak=False, sendmessages=False, read_message_history=False, read_messages=False)
  await member.add_roles(mutedRole, reason = reason)
  await ctx.send(f"Muted {member.mention} for reason {reason}")
  await member.send(f"You were muted in {guild.name} for {reason}")```

代码中有很多错误和拼写错误。 @commands.has_permissions(manage_messages=True)之前添加@bot.command(name=name)为我修复了它:

intent = discord.Intents(messages=True, message_content=True, guilds=True)
bot = commands.Bot(command_prefix="", description="", intents=intent) 
#See if you have "intents=<someintentobject>" and enable them in your Discord Developer Portal


@bot.event
async def on_ready():
    print("It's online!")


@bot.event
async def on_message(message: discord.Message):
    await bot.process_commands(message) #Let the commands process the message


@bot.command(name="mute") #This decorator tells that the mute function is a command the bot should process
@commands.has_permissions(manage_messages=True) #Run the mute command only if the message's author has the permission to manage messages
async def mute(ctx, member: discord.Member, reason=None): #Removed the extra arguments' asterisk (*)
#It's discord.Member, not discord.member
    guild = ctx.guild
    mutedRole = discord.utils.get(guild.roles, name="Muted") #utils, not Utils
    if not mutedRole == await guild.create_role(name="Muted"):
        for channel in guild.channels:
            await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=False,
                                          read_messages=False) #send_messages, not sendmessages
    await member.add_roles(mutedRole, reason=reason)
    await ctx.send(f"Muted {member.mention} for reason {reason}")
    await member.send(f"You were muted in {guild.name} for {reason}")

在您的on_message中添加await bot.process_commands(message)

我删除了* ( args ); 没有它它工作正常。 如果需要,您可以添加它。

请参阅此 kick 命令示例

 @bot.command()
 @commands.has_permissions(manage_messages=True)
 async def mute(ctx, member: discord.Member, reason=None):
    
    if member == ctx.author:
        embed = discord.Embed(
            color=discord.Colour.red(),
            description="You can't mute yourself !"
        )
        await ctx.send(embed=embed)
        return

    muterole = discord.utils.get(ctx.guild.roles, name="MUTED")
    if muterole is None:
        muterole = await ctx.guild.create_role(name="MUTED")

        for channel in ctx.guild.text_channels:
            await channel.set_permissions(muterole, overwrite=discord.PermissionOverwrite(send_messages=False))

    if muterole in member.roles:
        embed = discord.Embed(
            color=discord.Colour.red(),
            description=f'This member is already muted'
        )
        await ctx.send(embed=embed)
        return

    await member.add_roles(muterole)
    embed = discord.Embed(
            color=discord.Colour.green(),
            title=' Muted',
            description=f'**{member.mention}** is now  **permanently muted** reason: **{reason}**.'
        )
        embed.set_footer(text=f'Muted by {ctx.author}.')
        await ctx.send(embed=embed)

基本上“静音”命令的作用是创建或直接为成员分配角色,最重要的部分是正确配置静音角色。 还有更复杂的静音命令方法,例如临时静音命令,它允许您在模组指定的一定时间内静音某人,但这些天建议您使用timeout功能更容易和更有效。 但是你有它,一个带有适当检查的基本静音命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM