English 中文(简体)
难道有人会帮助我为一家不和谐的机器人在沙里建立一支/纯粹的指挥?
原标题:Could someone assist me in creating a /purge command in Python for a Discord bot?

我难以确定如何将我的分歧机器人编成密码,以明确某个特定用户(ex./purge [user] [num]的信息。 我已经能够从总体上明确信息,但能够把问题与具体用户联系起来。 我是新 brand,因此,我们非常赞赏任何帮助。

感谢!

#Purge command
@commands.has_permissions(administrator=True)
@bot.slash_command(
  name="purge", description="Purge messages from channel", 
  guild_ids=[1222114873962663967]
)
async def purge(ctx, number: int):
  await ctx.channel.purge(limit=number+1)
  await ctx.respond(f"Purged {number} messages")

该法典对打造电文做了罚款,但我对如何用任择的“用户”参数加以编码感到损失。

问题回答

TextChannel.purge() 方法还有<代码>check参数,通过该参数,你可以召集一个功能,过滤将删除的信息。 例如:

#Purge command
@commands.has_permissions(administrator=True)
@bot.slash_command(
  name="purge", description="Purge messages from channel", 
  guild_ids=[1222114873962663967]
)
async def purge(ctx, number: int, member: discord.Member = None):
  if member:
    deleted = await ctx.channel.purge(limit=number+1, check=lambda m: m.author == member)
  else:
    deleted = await ctx.channel.purge(limit=number+1)
  await ctx.respond(f"Purged {len(deleted)} messages")

You can loop through channel.history and check if the message.author is the one you specified with member which is of type discord.Member. If a member is provided it will only delete the messages from the member, otherwise all messages.

#Purge command
@commands.has_permissions(administrator=True)
@bot.slash_command(
  name="purge", description="Purge messages from channel", 
  guild_ids=[1222114873962663967]
)
async def purge(ctx, number: int, member: discord.Member=None):
  if member is not None:
     async for message in ctx.channel.history(limit=number):
        if message.author == member:
           await message.delete()
     return
  await ctx.channel.purge(limit=number+1)
  await ctx.respond(f"Purged {number} messages")




相关问题
How can I make my bot works in multiple servers?

I know that this question has been asked for discord.py, but couldn t find a discord.js one Currently I m working on a small Discord Bot, and I would like to make it with possible that it can be used ...

如何利用更好的不和谐来唤醒用户

我正在使我的想象力更好。 我的gin子将更新用户的“大约我”内容。 为了更新“我”的内容,我需要向不和谐提出要求。

热门标签