-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgiveaway.py
117 lines (91 loc) · 3.59 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
import re
import random
import asyncio
import discord
from discord.ext import commands
from utils.util import GetMessage
time_regex = re.compile(r"(?:(\d{1,5})(h|s|m|d))+?")
time_dict = {"h": 3600, "s": 1, "m": 60, "d": 86400}
def convert(argument):
args = argument.lower()
matches = re.findall(time_regex, args)
time = 0
for key, value in matches:
try:
time += time_dict[value] * float(key)
except KeyError:
raise commands.BadArgument(
f"{value} is an invalid time key! h|m|s|d are valid arguments"
)
except ValueError:
raise commands.BadArgument(f"{key} is not a number!")
return round(time)
class Giveaway(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(name="giveaway", description="Create a full giveaway!")
@commands.guild_only()
async def giveaway(self, ctx):
await ctx.send(
"Lets start this giveaway, answer the questions I ask and we shall proceed."
)
questionList = [
["What channel should it be in?", "Mention the channel"],
["How long should this giveaway last?", "`d|h|m|s`"],
["What are you giving away?", "I.E. Your soul hehe"],
]
answers = {}
for i, question in enumerate(questionList):
answer = await GetMessage(self.bot, ctx, question[0], question[1])
if not answer:
await ctx.send("You failed to answer, please answer quicker next time.")
return
answers[i] = answer
embed = discord.Embed(name="Giveaway content")
for key, value in answers.items():
embed.add_field(
name=f"Question: `{questionList[key][0]}`",
value=f"Answer: `{value}`",
inline=False,
)
m = await ctx.send("Are these all valid?", embed=embed)
await m.add_reaction("✅")
await m.add_reaction("🇽")
try:
reaction, member = await self.bot.wait_for(
"reaction_add",
timeout=60,
check=lambda reaction, user: user == ctx.author
and reaction.message.channel == ctx.channel,
)
except asyncio.TimeoutError:
await ctx.send("Confirmation Failure. Please try again.")
return
if str(reaction.emoji) not in ["✅", "🇽"] or str(reaction.emoji) == "🇽":
await ctx.send("Cancelling giveaway!")
return
channelId = re.findall(r"[0-9]+", answers[0])[0]
channel = self.bot.get_channel(int(channelId))
time = convert(answers[1])
giveawayEmbed = discord.Embed(
title="🎉 __**Giveaway**__ 🎉", description=answers[2]
)
giveawayEmbed.set_footer(
text=f"This giveaway ends {time} seconds from this message."
)
giveawayMessage = await channel.send(embed=giveawayEmbed)
await giveawayMessage.add_reaction("🎉")
await asyncio.sleep(time)
message = await channel.fetch_message(giveawayMessage.id)
users = await message.reactions[0].users().flatten()
users.pop(users.index(ctx.guild.me))
users.pop(users.index(ctx.author))
if len(users) == 0:
await channel.send("No winner was decided")
return
winner = random.choice(users)
await channel.send(
f"**Congrats {winner.mention}!**\nPlease contact {ctx.author.mention} about your prize."
)
def setup(bot):
bot.add_cog(Giveaway(bot))