I'm trying to create a discord.py giveaway command

Question:

I’ve tried this and i didn’t really get the logic through it, I need some help making it have:

  1. Emoji
  2. Message
  3. Setting the time

My Code:

@bot.command()
async def giveaway(ctx, msg, duration):
  embed=discord.Embed()
  embed.title=msg
  embed.description="React To Giveaway With   To Join."
  embed.set_footer(text="  MTND Bot Development")
  embed.color=0x00ffff
  msg = await ctx.send(embed=embed)
  await msg.add_reaction(' ')

Please help if you could.

Asked By: MTND Studios

||

Answers:

In order to make an giveaway command you need first of all, on going giveaways variables:

cmdsettings = {}
allowedRiggers = config.riggers
ongoingGiveaways = {}

like that then embed:

    actualTitle = 'Giveaway: ' + str(msg)
    embed = discord.Embed(color=0x0040ff,title=actualTitle)
    info = "React with   on this message to enter"
    
    embed.add_field(name='Message from creator', value=message, inline=False)
    embed.add_field(name='How to enter', value=info, inline=False)
    embed.add_field(name='Giveaway end date', value=endDate, inline=False)

that end date can be anything, but I am not going to show how to do it since i assume you know python. and them use those vars what i sent before, also this code is based on https://github.com/AnimeHasFallen/discordbot-giveaway/ so view the full source code there.

  • key39
Answered By: key39

Here is one.

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]

@client.command()
@commands.has_permissions(kick_members=True)
async def giveaway(ctx):
  await ctx.send("Let's start with this giveaway! Answer these questions within 15 seconds!")

  questions = ["Which channel should it be hosted in?", "What should be the duration of the giveaway? (s|m|h|d)", "What is the prize of the giveaway?"]

  answers = []

  def check(m):
    return m.author == ctx.author and m.channel == ctx.channel

  for i in questions:
    await ctx.send(i)

    try:
      msg = await client.wait_for('messsage', timeout=15.0, check=check)
    except asyncio.TimeoutError:
      await ctx.send('You didn't answer in time, please be quicker next time!')
      return
    else: 
      answers.append(msg.content)

  try:
    c_id = int(answers[0][2:-1])
  except:
    await ctx.send(f"You didn't mention a channel properly. Do it like this {ctx.channel.mention} next time.")
    return

  channel = client.get_channel(c_id)

  time = convert(answers[1])
  if time == -1:
    await ctx.send(f"You didn't answer with a proper unit. Use (s|m|h|d) next time!")
    return
  elif time == -2:
    await ctx.send(f"The time just be an integer. Please enter an integer next time.")
    return
  
  prize = answers[2]

  await ctx.send(f"The giveaway will be in {channel.mention} and will last {answers[1]} seconds!")

  embed = discord.embed(title = "Giveaway!", description = f"{prize}", color = ctx.author.color)

  embed.add_field(name = "Hosted by:", value = ctx.author.mention)

  embed.set_footer(text = f"Ends {answers[1]} from now!")

  my_msg = await channel.send(embed = embed)

  await my_msg.add_reaction(" ")

  await asyncio.sleep(time)

  new_msg = await channel.fetch_message(my_msg.id)

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

  winner = random.choice(users)

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


@client.command()
@commands.has_permissions(kick_members=True)
async def reroll(ctx, channel : discord.TextChannel, id_ : int):
  try:
    new_msg = await channel.fetch_message(id_)
  except:
    await ctx.send("The ID that was entered was incorrect, make sure you have entered the correct giveaway message ID.")
  users = await new_msg.reactions[0].users().flatten()
  users.pop(users.index(client.user))

  winner = random.choice(users)

  await channel.send(f"Congratulations the new winner is: {winner.mention} for the giveaway rerolled!")
Answered By: GarrettSucksAtCode

This is my whole code for my gcreate command

This is a command with embeds and steps

@client.command()
@commands.has_role("Giveaways")
async def gcreate(ctx):
  
  await ctx.send("Let's start with this giveaway!n`Answer these questions within 15 seconds!`")

  questions = ["**Which channel should it be hosted in?**", 
                    "**What should be the duration of the giveaway?** `(s|m|h|d)`",
                    "**What is the prize of the giveaway?**"]

  answers = []

  def check(m):
    return m.author == ctx.author and m.channel == ctx.channel 

    for i in questions:
        await ctx.send(i)

        try:
          msg = await client.wait_for('message', timeout=15.0, check=check)

        except asyncio.TimeoutError:
          await ctx.send('You didn't answer in time, please be quicker next time!')
          return
        else:
          answers.append(msg.content)
        
  try:
    c_id = int(answers[0][2:-1])
  except:
    await ctx.send(f"You didn't mention a channel properly. **Do it like this {ctx.channel.mention} next time.**")
    return

  channel = client.get_channel(c_id)

  time = convert(answers[1])
  if time == -1:
      await ctx.send("You didn't answer the time with a proper unit. Use `(s|m|h|d)` next time!")
      return
  elif time == -2:
      await ctx.send("The time must be a number. Please enter a number next time")
      return            
  elif time < 10:
      await ctx.send("Time Cannot Be Less Than 10s!!")
      return
      prize = answers[2]

      await ctx.send(f"The Giveaway will be in {channel.mention} and will last {answers[1]}!")

      embed = discord.Embed(title = "Giveaway!", description = "React To   To Enter The **Giveaway**!", color = discord.Color.blue())
      embed.add_field(name=":gift: Prize:-", value=f"{prize}", inline=False)
      embed.add_field(name=":timer: Ends In:-", value=f"{answers[1]}", inline=False)
      embed.set_thumbnail(url=ctx.guild.icon_url)
      embed.set_footer(text=f"Hosted By {ctx.author.name}", icon_url=ctx.author.avatar_url)

      my_msg = await channel.send(embed = embed)

      await my_msg.add_reaction(" ")

      await asyncio.sleep(time)

      new_msg = await channel.fetch_message(my_msg.id)

      users = await new_msg.reactions[0].users().flatten()
      users.pop(users.index(self.client.user))
      users.pop(users.index(ctx.author))

      if len(users) == 0:
          await channel.send("No Winner Was Decided!")
          return

      winner = random.choice(users)
      embed2 = discord.Embed(title = "Giveaway!", description ="**Giveaway Has Ended**!", color = discord.Color.blue())
      embed2.add_field(name=":gift: Prize:-", value=f"{prize}", inline=False)
      embed2.add_field(name=":trophy: Winner:-", value=f"{winner.mention}", inline=False)
      embed2.set_thumbnail(url=ctx.guild.icon_url)
      embed2.set_footer(text=f"Hosted By {ctx.author.name}", icon_url=ctx.author.avatar_url)    
      await my_msg.edit(embed=embed2)

      await channel.send(f"Congratulations! {winner.mention} Won The Prize:-`{prize}`!nhttps://discord.com/channels/{channel.guild.id}/{channel.id}/{my_msg.id}")


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]@client.command()
@commands.has_role("Giveaways")
async def gcreate(ctx):
  
  await ctx.send("Let's start with this giveaway!n`Answer these questions within 15 seconds!`")

  questions = ["**Which channel should it be hosted in?**", 
                    "**What should be the duration of the giveaway?** `(s|m|h|d)`",
                    "**What is the prize of the giveaway?**"]

  answers = []

  def check(m):
    return m.author == ctx.author and m.channel == ctx.channel 

    for i in questions:
        await ctx.send(i)

        try:
          msg = await client.wait_for('message', timeout=15.0, check=check)

        except asyncio.TimeoutError:
          await ctx.send('You didn't answer in time, please be quicker next time!')
          return
        else:
          answers.append(msg.content)
        
  try:
    c_id = int(answers[0][2:-1])
  except:
    await ctx.send(f"You didn't mention a channel properly. **Do it like this {ctx.channel.mention} next time.**")
    return

  channel = client.get_channel(c_id)

  time = convert(answers[1])
  if time == -1:
      await ctx.send("You didn't answer the time with a proper unit. Use `(s|m|h|d)` next time!")
      return
  elif time == -2:
      await ctx.send("The time must be a number. Please enter a number next time")
      return            
  elif time < 10:
      await ctx.send("Time Cannot Be Less Than 10s!!")
      return
      prize = answers[2]

      await ctx.send(f"The Giveaway will be in {channel.mention} and will last {answers[1]}!")

      embed = discord.Embed(title = "Giveaway!", description = "React To   To Enter The **Giveaway**!", color = discord.Color.blue())
      embed.add_field(name=":gift: Prize:-", value=f"{prize}", inline=False)
      embed.add_field(name=":timer: Ends In:-", value=f"{answers[1]}", inline=False)
      embed.set_thumbnail(url=ctx.guild.icon_url)
      embed.set_footer(text=f"Hosted By {ctx.author.name}", icon_url=ctx.author.avatar_url)

      my_msg = await channel.send(embed = embed)

      await my_msg.add_reaction(" ")

      await asyncio.sleep(time)

      new_msg = await channel.fetch_message(my_msg.id)

      users = await new_msg.reactions[0].users().flatten()
      users.pop(users.index(self.client.user))
      users.pop(users.index(ctx.author))

      if len(users) == 0:
          await channel.send("No Winner Was Decided!")
          return

      winner = random.choice(users)
      embed2 = discord.Embed(title = "Giveaway!", description ="**Giveaway Has Ended**!", color = discord.Color.blue())
      embed2.add_field(name=":gift: Prize:-", value=f"{prize}", inline=False)
      embed2.add_field(name=":trophy: Winner:-", value=f"{winner.mention}", inline=False)
      embed2.set_thumbnail(url=ctx.guild.icon_url)
      embed2.set_footer(text=f"Hosted By {ctx.author.name}", icon_url=ctx.author.avatar_url)    
      await my_msg.edit(embed=embed2)

      await channel.send(f"Congratulations! {winner.mention} Won The Prize:-`{prize}`!nhttps://discord.com/channels/{channel.guild.id}/{channel.id}/{my_msg.id}")


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]**strong text**
    ```
Answered By: Demonic