DISCORD.PY Creating an end Giveaway Command

Viewed 105

I am working on a giveaway bot and after doing the start and reroll command I have run into the end command , which i cannot fully grasp how to do it. I thought to register something when the giveaway is created (msgID of the giveaway im registering) in my aiosqlite database and for the end function , i could be able to fetch it and stop the giveaway. Now here is the thing, i cant think of a function or a task that will end the giveaway or somehow just end the duration. For reference here is my start command :

class Start(commands.Cog):
    def __init__(self, client):
        self.client = client

    def convert(self, timer):
        pos = ["s", "m", "h", "d"]
        time_dict = {"s" : 1, "m" : 60, "h" : 3600, "d" : 3600*24}
        unit = timer[-1]

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

        return val * time_dict[unit]
    
    @commands.command()
    async def start(self, ctx, duration, winners: str, *, prize):

      timer = (self.convert(duration))
      winners = int(winners.replace("w",""))
      await ctx.message.delete()

      timestamp = time.time() + timer
      epoch_time = int((time.time() + timer))

      embed = discord.Embed(title = f"{prize}", description = f'React with  to enter\nEnds: <t:{epoch_time}:R> (<t:{epoch_time}>)\nHosted by {ctx.author.mention}\n', color = 
      ctx.author.color, timestamp=(datetime.datetime.utcfromtimestamp(timestamp)))
      embed.set_footer(text=f'Winners : {winners} | Ends at \u200b')
      gaw_msg = await ctx.send(content = "<:spadess:939938117736091678> **GIVEAWAY** <:spadess:939938117736091678>",embed=embed)

      await gaw_msg.add_reaction("") 

      db = await aiosqlite.connect("main.db")
      cursor = await db.cursor()
      await cursor.execute(f'SELECT * FROM storingStuff WHERE msgID = {gaw_msg.id}')
      data = await cursor.fetchone()

      if data is None:
        await cursor.execute(f'INSERT INTO storingStuff (msgID, guildID) VALUES({gaw_msg.guild.id} , {gaw_msg.id})')
        await db.commit()

        await cursor.close()
        await db.close()


      await asyncio.sleep(timer)

      new_msg = await ctx.channel.fetch_message(gaw_msg.id)

      users_mention = []     
      for i in range(winners):
        users = await new_msg.reactions[0].users().flatten()
        users.pop(users.index(self.client.user))
        winner = random.choice(users)
        users_mention.append(winner.mention)
        users.remove(winner)

        displayed_winners = ",".join(users_mention)

      endembed = discord.Embed(title=f"{prize}", description=f"Winner: {displayed_winners}\nHosted by: {ctx.author.mention}", color = ctx.author.color, timestamp=(datetime.datetime.utcfromtimestamp(timestamp)))
      endembed.set_footer(text= 'Ended at \u200b')
      await gaw_msg.edit(content = "<:done:939940228746072096> **GIVEAWAY ENDED** <:done:939940228746072096>",embed=endembed)
      await ctx.send(f"Congragulations {displayed_winners}! You won the **{prize}**.\n{gaw_msg.jump_url}")
      

def setup(client):
    client.add_cog(Start(client))

Any help with the case would be appreciated , or any code reference as I'm pretty new. Thank you for spending your time and reading this.

0 Answers
Related