from discord.
ext import commands, tasks
import datetime, pytz, time as t
from [Link] import Button, Select, View
import aiosqlite, random, typing
import sqlite3
import asyncio
import discord, logging
from [Link] import get
connection = [Link]('[Link]')
cursor = [Link]()
[Link]('''CREATE TABLE IF NOT EXISTS Giveaway (
guild_id INTEGER,
host_id INTEGER,
start_time TIMESTAMP,
ends_at TIMESTAMP,
prize TEXT,
winners INTEGER,
message_id INTEGER,
channel_id INTEGER,
PRIMARY KEY (guild_id, message_id)
)''')
[Link]()
[Link]()
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
return val * time_dict[unit]
def WinnerConverter(winner):
try:
int(winner)
except ValueError:
try:
return int(winner[:-1])
except:
return -4
return winner
class Giveaway([Link]):
def __init__(self, bot):
[Link] = bot
async def cog_load(self) -> None:
[Link] = await [Link]('[Link]')
[Link] = await [Link]()
[Link]()
async def cog_unload(self) -> None:
await [Link]()
@commands.hybrid_command(description="Starts a new giveaway.")
@[Link](1, 5, [Link])
@commands.check_any(commands.is_owner(),
commands.has_permissions(manage_guild=True))
async def gstart(self, ctx,
time,
winners: int,
*,
prize: str):
await [Link]("SELECT message_id, channel_id FROM Giveaway
WHERE guild_id = ?", ([Link],))
re = await [Link]()
if winners >= 15:
embed = [Link](description=f"<:danger:1324290453830369331>
Cannot exceed more than 15 winners.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
g_list = [i[0] for i in re]
if len(g_list) >= 5:
embed = [Link](description=f"<:danger:1324290453830369331> The
maximum limit of giveaways for this guild has been reached.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
converted = [Link](time)
if converted / 60 >= 50400:
embed = [Link](description=f"<:danger:1324290453830369331> Time
cannot exceed 31 days!", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
if converted == -1:
embed = [Link](description=f"<:danger:1324290453830369331>
Invalid time format", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
if converted == -2:
embed = [Link](description=f"<:danger:1324290453830369331>
Invalid time format. Please provide the time in numbers.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
ends = ([Link]().timestamp() + converted)
embed = [Link](description=f"Winner(s): **{winners}**\nReact with 🎉
to participate!\nEnds <t:{round(ends)}:R> (<t:{round(ends)}:f>)\n\nHosted by
{[Link]}", color=0x2f3136)
ends1 = [Link]() + [Link](seconds=converted)
ends_utc = [Link](tzinfo=[Link])
[Link] = [Link] = ends_utc
if [Link]:
embed.set_author(icon_url=[Link], name=prize)
else:
embed.set_author(name=prize,
icon_url=[Link].display_avatar.url)
embed.set_footer(text=f"Ends at")
message = await [Link]("🎁 **GIVEAWAY** 🎁", embed=embed)
try:
await [Link]()
except:
pass
await [Link]("INSERT INTO Giveaway(guild_id, host_id,
start_time, ends_at, prize, winners, message_id, channel_id)
VALUES(?, ?, ?, ?, ?, ?, ?, ?)", ([Link], [Link],
[Link](), ends, prize, winners, [Link], [Link]))
await message.add_reaction("🎉")
await [Link]()
@[Link](seconds=5)
async def GiveawayEnd(self):
await [Link]("SELECT ends_at, guild_id, message_id, host_id,
winners, prize, channel_id FROM Giveaway WHERE channel_id IS NOT NULL")
ends_raw = await [Link]()
current_time = [Link]().timestamp()
for giveaway in ends_raw:
if int(current_time) >= round(float(giveaway[0])):
guild = [Link].get_guild(int(giveaway[1]))
channel = [Link].get_channel(int(giveaway[6]))
if channel is not None:
try:
message = await channel.fetch_message(int(giveaway[2]))
except [Link]:
continue
users = [[Link] async for i in [Link][0].users()]
[Link]([Link])
if len(users) < 1:
await [Link](f"No one won the **{giveaway[5]}**
giveaway, due to not enough participants.")
await [Link]("DELETE FROM Giveaway WHERE
message_id = ? AND guild_id = ?", ([Link], [Link]))
return
winner = ', '.join(f'<@!{i}>' for i in [Link](users,
k=int(giveaway[4])))
embed = [Link](
description=f"Ended <t:{int(current_time)}:R>\nHosted by
<@{int(giveaway[3])}>\nWinner(s): {winner}",
color=0x2f3136
)
[Link] = [Link]()
embed.set_author(name=giveaway[5])
embed.set_footer(text=f"Ended at")
try:
await [Link](content="🎁 **GIVEAWAY ENDED** 🎁",
embed=embed)
except:
pass
try:
await [Link](f"{winner} 🎉 Congratulations! you won
**{giveaway[5]}!**, Hosted by <@{int(giveaway[3])}>")
except:
pass
await [Link]("DELETE FROM Giveaway WHERE
message_id = ? AND guild_id = ?", ([Link], [Link]))
#print(f"[Natural] Giveaway Ended - {[Link]}
({giveaway[5]})")
await [Link]()
@[Link]("on_message_delete")
async def GiveawayMessageDelete(self, message):
await [Link]("SELECT message_id FROM Giveaway WHERE guild_id =
?", ([Link],))
re = await [Link]()
if [Link] != [Link]:
return
if re is not None:
if [Link] == int(re[0]):
await [Link]("DELETE FROM Giveaway WHERE channel_id
= ? AND message_id = ? AND guild_id = ?", ([Link], [Link],
[Link]))
print(f"Giveaway message deleted in {[Link]} -
{[Link]}")
await [Link]()
@commands.hybrid_command(name="gend", description="Ends a giveaway early.")
@commands.check_any(commands.is_owner(),
commands.has_permissions(manage_guild=True))
async def gend(self, ctx, message_id = None):
if message_id:
try:
int(message_id)
except ValueError:
embed = [Link](description=f"<:danger:1324290453830369331>
The provided message ID is invalid.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
if message_id is not None:
current_time = [Link]().timestamp()
await [Link]('SELECT ends_at, guild_id, message_id,
host_id, winners, prize, channel_id FROM Giveaway WHERE message_id = ?',
(int(message_id),))
re = await [Link]()
if re is None:
embed = [Link](description=f"<:danger:1324290453830369331>
The giveaway was not found.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
ch = [Link].get_channel(int(re[6]))
message = await ch.fetch_message(int(message_id))
users = [[Link] async for i in [Link][0].users()]
[Link]([Link])
if len(users) < 1:
await [Link](f"<:tick:1324284313885212714> Successfully ended the
giveaway in <#{int(re[6])}>")
await [Link](f"No one won the **{re[5]}** giveaway, due to
not enough participants.")
await [Link]("DELETE FROM Giveaway WHERE message_id
= ? AND guild_id = ?", ([Link], [Link]))
return
winner = ', '.join(f'<@!{i}>' for i in [Link](users,
k=int(re[4])))
embed = [Link](
description=f"Ended <t:{int(current_time)}:R>\nHosted by
<@{int(re[3])}>\nWinner(s): {winner}",
color=0x2f3136
)
[Link] = [Link]()
embed.set_author(name=re[5],
icon_url=[Link])
embed.set_footer(text=f"Ended at")
await [Link](content="🎁 **GIVEAWAY ENDED** 🎁", embed=embed)
if int([Link]) != int(re[6]):
await [Link](f"<:tick:1324284313885212714> Successfully ended the
giveaway in <#{int(re[6])}>")
await [Link](f"{winner} 🎉 Congratulations! you won **{re[5]}!**,
Hosted by <@{int(re[3])}>")
await [Link]("DELETE FROM Giveaway WHERE message_id = ?
AND guild_id = ?", ([Link], [Link]))
#print(f"[Gend] Giveaway Ended - {[Link]}
({[Link]}) - ({re[5]})")
elif [Link]:
await [Link]('SELECT ends_at, guild_id, message_id,
host_id, winners, prize, channel_id FROM Giveaway WHERE message_id = ?',
([Link],))
re = await [Link]()
if re is None:
return await [Link](f"<:danger:1324290453830369331> The giveaway
was not found.")
current_time = [Link]().timestamp()
message = await ctx.fetch_message([Link].message_id)
users = [[Link] async for i in [Link][0].users()]
try: [Link]([Link])
except: pass
if len(users) < 1:
await [Link](f"No one won the **{re[5]}** giveaway, due to
not enough participants.")
await [Link]("DELETE FROM Giveaway WHERE message_id
= ? AND guild_id = ?", ([Link], [Link]))
return
winner = ', '.join(f'<@!{i}>' for i in [Link](users,
k=int(re[4])))
embed = [Link](
description=f"Ended <t:{int(current_time)}:R>\nHosted by
<@{int(re[3])}>\nWinner(s): {winner}",
color=0x2f3136
)
[Link] = [Link]()
embed.set_author(name=re[5],
icon_url=[Link])
embed.set_footer(text=f"Ended at")
await [Link](content="🎁 **GIVEAWAY ENDED** 🎁", embed=embed)
await [Link](f"{winner} 🎉 Congratulations! you won **{re[5]}!**,
Hosted by <@{int(re[3])}>")
await [Link]("DELETE FROM Giveaway WHERE message_id = ?
AND guild_id = ?", ([Link], [Link]))
#print(f"[Gend] Giveaway Ended - {[Link]}
({[Link]}) - ({re[5]})")
else:
await [Link]("Please reply to the giveaway message or provide the
giveaway ID.")
await [Link]()
@commands.hybrid_command(description="Rerolls a giveaway. | Reply to the
giveaway message.")
@commands.check_any(commands.is_owner(),
commands.has_permissions(manage_guild=True))
async def greroll(self, ctx, message_id: [Link][int] = None):
if not [Link]:
embed = [Link](description=f"<:danger:1324290453830369331> Reply
with the giveaway message to reroll.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
if [Link]:
message_id = [Link]
message = await ctx.fetch_message(message_id)
if [Link] != [Link]:
embed = [Link](description=f"<:danger:1324290453830369331> The
giveaway was not found.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
await [Link](f"SELECT message_id FROM Giveaway WHERE
message_id = ?", ([Link],))
re = await [Link]()
if re is not None:
embed = [Link](description=f"<:danger:1324290453830369331> The
giveaway is currently running. Please use the gend command instead to end the
giveaway.", color=0x2f3136)
message = await [Link](embed=embed)
await [Link](5)
await [Link]()
return
users = [[Link] async for i in [Link][0].users()]
[Link]([Link])
if len(users) < 1:
await [Link](f"No one won the **{re[5]}** giveaway, due to not
enough participants.")
return
winners = [Link](users, k=1)
await [Link](f"🎉 The new winner is "+", ".join(f"<@{i}>" for i in
winners)+". Congratulations!")
await [Link]()
def convert(self, 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 -1
try:
val = int(time[:-1])
except ValueError:
return -2
return val * time_dict[unit]