This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgiveaway.py
370 lines (331 loc) · 15.6 KB
/
giveaway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
import asyncio
import datetime
import logging
import random
import discord
from discord.ext import commands
from classes.bot import SnedBot
async def has_owner(ctx):
return await ctx.bot.custom_checks.has_owner(ctx)
logger = logging.getLogger(__name__)
class Giveaway(commands.Cog):
"""
Miscellanious featureset to create giveaways. It 100% uses the existing timer structure,
with no additional database/storage features. I wrote it in like 3 hours so it might suck. :)
"""
def __init__(self, bot: SnedBot):
self.bot = bot
self._ = self.bot.get_localization("giveaway", self.bot.lang)
async def cog_check(self, ctx):
return await ctx.bot.custom_checks.has_permissions(
ctx, "giveaway"
) or await ctx.bot.custom_checks.has_permissions(ctx, "mod_permitted")
@commands.group(
help="Create and manage giveaways. See sub-commands for more.",
description="Create and manage giveaways on this server. See sub-commands below.",
usage="giveaway [subcommand]",
invoke_without_command=True,
case_insensitive=True,
)
async def giveaway(self, ctx):
await ctx.send_help(ctx.command)
@giveaway.command(
name="create",
help="Starts the giveaway creation wizard.",
description="Starts the giveaway creation wizard to help you set up a new giveaway.",
usage="giveaway create",
)
async def giveaway_create(self, ctx):
cogs = await self.bot.current_cogs()
if "Timers" not in cogs:
embed = discord.Embed(
title="❌ Missing module",
description="This setup requires the extension `timers` to be active.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
embed = discord.Embed(
title="🛠️ Giveaway creator",
description="Please mention the channel where the giveaway message should be sent!",
color=self.bot.embed_blue,
)
await ctx.channel.send(embed=embed)
def check(message):
return message.author == ctx.author and message.channel.id == ctx.channel.id
try:
message = await self.bot.wait_for("message", timeout=60.0, check=check)
giveaway_channel = await commands.TextChannelConverter().convert(ctx, message.content)
embed = discord.Embed(
title="🛠️ Giveaway creator",
description="Now specify the amount of winners by typing in a number!",
color=self.bot.embed_blue,
)
await ctx.channel.send(embed=embed)
message = await self.bot.wait_for("message", timeout=60.0, check=check)
try:
winners = int(message.content)
except (ValueError, TypeError):
embed = discord.Embed(
title="❌ Error: Invalid value",
description="Invalid value entered. Operation cancelled.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
embed = discord.Embed(
title="🛠️ Giveaway creator",
description="How long should the giveaway last? Examples: `12 hours` or `7 days and 5 minutes`",
color=self.bot.embed_blue,
)
await ctx.channel.send(embed=embed)
message = await self.bot.wait_for("message", timeout=60.0, check=check)
try:
time, timestr = await self.bot.get_cog("Timers").converttime(message.content)
except ValueError:
embed = discord.Embed(
title="❌ Error: Invalid value",
description="Invalid time entered. Operation cancelled.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
embed = discord.Embed(
title="🛠️ Giveaway creator",
description="Now, as a last step, type in what you are going to give away! This will show up in the message part of the giveaway.",
color=self.bot.embed_blue,
)
await ctx.channel.send(embed=embed)
message = await self.bot.wait_for("message", timeout=60.0, check=check)
giveaway_text = message.content
try:
embed = discord.Embed(
title="🎉 Giveaway!",
description=f"""**{giveaway_text}**
**-------------**
**End date:** {discord.utils.format_dt(time, style='F')}
**Number of winners:** `{winners}`""",
color=0xD76B00,
)
embed.set_footer(text=f"Hosted by {ctx.author}", icon_url=ctx.author.avatar.url)
giveaway_msg = await giveaway_channel.send(embed=embed)
await giveaway_msg.add_reaction("🎉")
embed = discord.Embed(
title="✅ " + "Giveaway created",
description="Giveaway created successfully!",
color=self.bot.embed_green,
)
await ctx.send(embed=embed)
except Exception as error:
embed = discord.Embed(
title="❌ Error: Cannot send message",
description=f"Unable to send the giveaway message to the given channel.\n**Error:** ```{error}```",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
await self.bot.get_cog("Timers").create_timer(
expires=time,
event="giveaway",
guild_id=ctx.guild.id,
user_id=ctx.author.id,
channel_id=giveaway_channel.id,
notes=f"{giveaway_msg.id}\n{winners}",
)
except commands.ChannelNotFound:
embed = discord.Embed(
title="❌ Error: Channel not found",
description="Unable to locate channel. Operation cancelled.",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
except asyncio.TimeoutError:
embed = discord.Embed(
title="🕘 Error: Timed out",
description="Your session has expired. Execute the command again!",
color=self.bot.error_color,
)
await ctx.channel.send(embed=embed)
return
@giveaway.command(
name="list",
usage="giveaway list",
help="Lists all running giveaways for the server.",
description="Lists all running giveaways for this server, and their ID. Displays only up to 10 entries.",
)
@commands.guild_only()
async def giveaway_list(self, ctx):
results = await self.bot.pool.fetch(
"""SELECT * FROM timers WHERE guild_id = $1 AND user_id = $2 AND event = $3 ORDER BY expires LIMIT 10""",
ctx.guild.id,
ctx.author.id,
"giveaway",
)
giveaways = []
list_str = ""
if len(results) != 0:
for result in results:
time = datetime.datetime.fromtimestamp(result.get("expires"))
channel = self.bot.get_channel(result.get("channel_id"))
list_str = (
list_str
+ f"**ID: {result.get('id')}** - {channel.mention} - Concludes: {discord.utils.format_dt(time)}\n"
)
else:
list_str = self._(
"There are currently no running giveaways on this server. You can create one via `{prefix}giveaway create`!"
).format(prefix=ctx.prefix)
embed = discord.Embed(
title="🎉 " + self._("List of giveaways:"),
description=list_str,
color=self.bot.embed_blue,
)
embed = self.bot.add_embed_footer(ctx, embed)
await ctx.send(embed=embed)
@giveaway.command(
name="cancel",
aliases=["del", "delete", "remove"],
usage="giveaway delete <giveaway_ID>",
help="Cancels a running giveaway.",
description="Cancels a giveaway by it's ID, which you can obtain via the `giveaway list` command.",
)
@commands.guild_only()
async def giveaway_delete(self, ctx, ID: int):
async with self.bot.pool.acquire() as con:
result = await con.fetch("""SELECT * FROM timers WHERE event = $1 AND id = $2""", "giveaway", ID)
if result:
await con.execute(
"""DELETE FROM timers WHERE event = $1 AND id = $2""",
"giveaway",
ID,
)
embed = discord.Embed(
title="✅ " + self._("Giveaway deleted"),
description=self._("Giveaway **{ID}** has been cancelled and deleted.").format(ID=ID),
color=self.bot.embed_green,
)
await ctx.send(embed=embed)
# If we just deleted the currently running timer, then we re-evaluate to find the next timer.
if self.bot.get_cog("Timers").current_timer and self.bot.get_cog("Timers").current_timer.id == int(ID):
self.bot.get_cog("Timers").currenttask.cancel()
self.bot.get_cog("Timers").currenttask = self.bot.get_cog("Timers").bot.loop.create_task(
self.bot.get_cog("Timers").dispatch_timers()
)
else:
embed = discord.Embed(
title="❌ " + self._("Giveaway not found"),
description=self._("Cannot find giveaway with ID **{ID}**.").format(ID=ID),
color=self.bot.error_color,
)
embed = self.bot.add_embed_footer(ctx, embed)
await ctx.send(embed=embed)
@giveaway.command(
name="end",
aliases=["terminate"],
usage="giveaway end <giveaway_ID>",
help="Forces a running giveaway to end.",
description="Forces a running giveaway to conclude, causing the winners to be calculated immediately.",
)
@commands.guild_only()
async def giveaway_terminate(self, ctx, ID: int):
async with self.bot.pool.acquire() as con:
result = await con.fetch("""SELECT * FROM timers WHERE event = $1 AND id = $2""", "giveaway", ID)
if result:
await con.execute(
"""DELETE FROM timers WHERE event = $1 AND id = $2""",
"giveaway",
ID,
)
embed = discord.Embed(
title="✅ " + self._("Giveaway terminated"),
description=self._(
"Giveaway **{ID}** has been forced to end, and winners have been calculated."
).format(ID=ID),
color=self.bot.embed_green,
)
await ctx.send(embed=embed)
# If we just deleted the currently running timer, then we re-evaluate to find the next timer.
if self.bot.get_cog("Timers").current_timer and self.bot.get_cog("Timers").current_timer.id == int(ID):
self.bot.get_cog("Timers").currenttask.cancel()
self.bot.get_cog("Timers").currenttask = self.bot.get_cog("Timers").bot.loop.create_task(
self.bot.get_cog("Timers").dispatch_timers()
)
# Calculating the winners
channel = self.bot.get_channel(result[0].get("channel_id"))
message = await channel.fetch_message(int(result[0].get("notes").split("\n")[0]))
embed = message.embeds[0]
for reaction in message.reactions:
if reaction.emoji == "🎉":
tada_react = reaction
users = await tada_react.users().flatten()
for user in users:
if user.bot:
users.remove(user)
winner_count = int(result[0].get("notes").split("\n")[1])
if len(users) >= winner_count:
winners = []
for i in range(0, winner_count):
winners.append(random.choice(users))
users.remove(winners[len(winners) - 1])
winners_str = "\n".join(
[f"{winner.mention} `({winner.name}#{winner.discriminator})`" for winner in winners]
)
embed.description = f"{embed.description}\n\n**Winners:**\n {winners_str}"
await message.edit(embed=embed)
winner_mentions = ", ".join([winner.mention for winner in winners])
await channel.send(
f"Giveaway was forced to terminate by a moderator.\n{winner_mentions} **won the giveaway!** 🎉"
)
else:
err_embed = discord.Embed(
title="🎉 Not enough participants",
description="The giveaway was forced to end by a moderator with insufficient participants.",
color=self.bot.error_color,
)
err_embed.set_footer(text="Hint: You could try lowering the amount of winners.")
await channel.send(embed=err_embed)
else:
embed = discord.Embed(
title="❌ " + self._("Giveaway not found"),
description=self._("Cannot find giveaway with ID **{ID}**.").format(ID=ID),
color=self.bot.error_color,
)
embed = self.bot.add_embed_footer(ctx, embed)
await ctx.send(embed=embed)
@commands.Cog.listener()
async def on_giveaway_timer_complete(self, timer):
channel = self.bot.get_channel(timer.channel_id)
message = await channel.fetch_message(int(timer.notes.split("\n")[0]))
embed = message.embeds[0]
for reaction in message.reactions:
if reaction.emoji == "🎉":
tada_react = reaction
users = await tada_react.users().flatten()
for user in users:
if user.bot:
users.remove(user)
winner_count = int(timer.notes.split("\n")[1])
if len(users) >= winner_count:
winners = []
for i in range(0, winner_count):
winners.append(random.choice(users))
users.remove(winners[len(winners) - 1])
winners_str = "\n".join(
[f"{winner.mention} `({winner.name}#{winner.discriminator})`" for winner in winners]
)
embed.description = f"{embed.description}\n\n**Winners:**\n {winners_str}"
await message.edit(embed=embed)
winner_mentions = ", ".join([winner.mention for winner in winners])
await channel.send(f"{winner_mentions} **won the giveaway!** 🎉")
else:
err_embed = discord.Embed(
title="🎉 Not enough participants",
description="The giveaway ended with insufficient participants.",
color=self.bot.error_color,
)
err_embed.set_footer(text="Hint: You could try lowering the amount of winners.")
await channel.send(embed=err_embed)
def setup(bot: SnedBot):
logger.info("Adding cog: Giveaway...")
bot.add_cog(Giveaway(bot))