Message
Message
connection = sqlite3.connect('giveaways.db')
cursor = connection.cursor()
connection.commit()
connection.close()
def convert(time):
pos = ["s","m","h","d"]
time_dict = {"s" : 1, "m" : 60, "h" : 3600 , "d" : 86400 , "f" : 259200}
unit = time[-1]
if unit not in pos:
return
try:
val = int(time[:-1])
except ValueError:
return
def WinnerConverter(winner):
try:
int(winner)
except ValueError:
try:
return int(winner[:-1])
except:
return -4
return winner
class Giveaway(commands.Cog):
def __init__(self, bot):
self.bot = bot
converted = self.convert(time)
if converted / 60 >= 50400:
embed = discord.Embed(description=f"<:danger:1324290453830369331> Time
cannot exceed 31 days!", color=0x2f3136)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.delete()
return
if converted == -1:
embed = discord.Embed(description=f"<:danger:1324290453830369331>
Invalid time format", color=0x2f3136)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.delete()
return
if converted == -2:
embed = discord.Embed(description=f"<:danger:1324290453830369331>
Invalid time format. Please provide the time in numbers.", color=0x2f3136)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.delete()
return
await message.add_reaction("🎉")
await self.connection.commit()
@tasks.loop(seconds=5)
async def GiveawayEnd(self):
await self.cursor.execute("SELECT ends_at, guild_id, message_id, host_id,
winners, prize, channel_id FROM Giveaway WHERE channel_id IS NOT NULL")
ends_raw = await self.cursor.fetchall()
current_time = datetime.datetime.now().timestamp()
embed = discord.Embed(
description=f"Ended <t:{int(current_time)}:R>\nHosted by
<@{int(giveaway[3])}>\nWinner(s): {winner}",
color=0x2f3136
)
embed.timestamp = discord.utils.utcnow()
embed.set_author(name=giveaway[5])
embed.set_footer(text=f"Ended at")
try:
await message.edit(content="🎁 **GIVEAWAY ENDED** 🎁",
embed=embed)
except:
pass
try:
await message.reply(f"{winner} 🎉 Congratulations! you won
**{giveaway[5]}!**, Hosted by <@{int(giveaway[3])}>")
except:
pass
await self.cursor.execute("DELETE FROM Giveaway WHERE
message_id = ? AND guild_id = ?", (message.id, message.guild.id))
#print(f"[Natural] Giveaway Ended - {guild.id}
({giveaway[5]})")
await self.connection.commit()
@commands.Cog.listener("on_message_delete")
async def GiveawayMessageDelete(self, message):
await self.cursor.execute("SELECT message_id FROM Giveaway WHERE guild_id =
?", (message.guild.id,))
re = await self.cursor.fetchone()
if message.author != self.bot.user:
return
if re is not None:
if message.id == int(re[0]):
await self.cursor.execute("DELETE FROM Giveaway WHERE channel_id
= ? AND message_id = ? AND guild_id = ?", (message.channel.id, message.id,
message.guild.id))
if re is None:
embed = discord.Embed(description=f"<:danger:1324290453830369331>
The giveaway was not found.", color=0x2f3136)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.delete()
return
ch = self.bot.get_channel(int(re[6]))
message = await ch.fetch_message(int(message_id))
if len(users) < 1:
await ctx.send(f"<:tick:1324284313885212714> Successfully ended the
giveaway in <#{int(re[6])}>")
await message.reply(f"No one won the **{re[5]}** giveaway, due to
not enough participants.")
await self.cursor.execute("DELETE FROM Giveaway WHERE message_id
= ? AND guild_id = ?", (message.id, message.guild.id))
return
embed = discord.Embed(
description=f"Ended <t:{int(current_time)}:R>\nHosted by
<@{int(re[3])}>\nWinner(s): {winner}",
color=0x2f3136
)
embed.timestamp = discord.utils.utcnow()
embed.set_author(name=re[5],
icon_url=ctx.guild.icon.url)
embed.set_footer(text=f"Ended at")
elif ctx.message.reference:
await self.cursor.execute('SELECT ends_at, guild_id, message_id,
host_id, winners, prize, channel_id FROM Giveaway WHERE message_id = ?',
(ctx.message.reference.resolved.id,))
re = await self.cursor.fetchone()
if re is None:
return await ctx.send(f"<:danger:1324290453830369331> The giveaway
was not found.")
current_time = datetime.datetime.now().timestamp()
if len(users) < 1:
await message.reply(f"No one won the **{re[5]}** giveaway, due to
not enough participants.")
await self.cursor.execute("DELETE FROM Giveaway WHERE message_id
= ? AND guild_id = ?", (message.id, message.guild.id))
return
embed = discord.Embed(
description=f"Ended <t:{int(current_time)}:R>\nHosted by
<@{int(re[3])}>\nWinner(s): {winner}",
color=0x2f3136
)
embed.timestamp = discord.utils.utcnow()
embed.set_author(name=re[5],
icon_url=ctx.guild.icon.url)
embed.set_footer(text=f"Ended at")
if ctx.message.reference:
message_id = ctx.message.reference.resolved.id
if ctx.message.reference.resolved.author.id != self.bot.user.id:
embed = discord.Embed(description=f"<:danger:1324290453830369331> The
giveaway was not found.", color=0x2f3136)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.delete()
return
if re is not None:
embed = discord.Embed(description=f"<:danger:1324290453830369331> The
giveaway is currently running. Please use the gend command instead to end the
giveaway.", color=0x2f3136)
message = await ctx.send(embed=embed)
await asyncio.sleep(5)
await message.delete()
return
if len(users) < 1:
await message.reply(f"No one won the **{re[5]}** giveaway, due to not
enough participants.")
return
unit = time[-1]
if unit not in pos:
return -1
try:
val = int(time[:-1])
except ValueError:
return -2