如何让机器人在用 python 回复我后删除你的命令

时间:2021-06-07 09:26:55

标签: python discord discord.py

我正在进行赠品命令调用 =gstart <time> <prize,赠品将保留在我使用此命令的频道中,并且在发送嵌入赠品消息后,它将删除 gstart 命令。代码如下:

@bot.command()
async def gstart(ctx,time,*,prize):
    time = convert(time)
    if time == -1:
        await ctx.send("Error, can't identify time unit")
    if time == -2:
        await ctx.send("Error, can't identify time value")
    embed = discord.Embed(title=prize, description = f"React with :tada: to enter\nHosted by: {ctx.author.mention}", color=discord.Color.red())
    embed.set_footer(text = f"Ends at {time} from now!")

    #delete-command


    await ctx.send(":tada:  **GIVEAWAY**  :tada:")

    my_msg = await ctx.send(embed = embed)
    await my_msg.add_reaction("?")
    await asyncio.sleep(time)

    new_msg = await ctx.fetch_message(my_msg.id)

    users = await new_msg.reactions[0].users().flatten()
    users.pop(users.index(bot.user))

    winner = random.choice(users)

    await ctx.send(f"Congratulations {winner.mention}! You won the {prize}!")

    msg_link = my_msg.jump_url
    

    winner_embed = discord.Embed(title=f"Hey {winner}",description=f"You just won a [giveaway]({msg_link}) in  {ctx.guild}",color=discord.Color.red())
    winner_embed.add_field(name=prize,value=f"Hosted by: {ctx.author.mention}",inline=True)
    await winner.send(embed=winner_embed)

顺便说一下,convert() 函数是这样的:

def convert(time):
  pos = ["s","m","h","d"]

  time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}

  unit = time[-1]

  if unit not in pos:
    return -1
  try:
    val = int(time[:-1])
  except:
    return -2
  
  return val * time_dict[unit]

1 个答案:

答案 0 :(得分:1)

使用 discord.Context 对象回复您并删除用户发送的消息。

示例:

import asyncio
@bot.command()
async def deleteCommandAfterSent(ctx):
    await ctx.send("blablabla ") #REPLY MESSAGE
    await asyncio.sleep(1)
    await ctx.message.delete()