-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgiveaway-botV102.py
135 lines (107 loc) · 6.51 KB
/
giveaway-botV102.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
import discord
from discord.ext import commands
import asyncio
import random
import datetime
import os
# Prefix is set to g! for all commands
client = commands.Bot(command_prefix = 'g!')
@client.event
async def on_ready():
# Prints a message when the bot is online and functioning
await client.change_presence(status=discord.Status.online, activity = discord.Game(name=f'g!helpme for a list of commands! 🥳 🎉 Currently in {len(client.guilds)} servers! 🎉'))
print('Ready to giveaway!')
@client.command()
async def version(ctx):
# Version command that contains the current version number and recent changes made
ver = discord.Embed(color = 0x7289da)
ver.set_author(name = 'Update Notes', icon_url= '')
ver.add_field(name = 'Version: 1.02', value = f'• The giveaway command has seen a complete overhaul, now allowing hosts to run giveaways for specific channels without the users seeing the commands.\n• Fixed various grammar and spelling mistakes.\n• Fixed the bots status.', inline= False)
await ctx.send(embed = ver)
@client.command()
async def helpme(ctx):
# Help command that lists the current available commands and describes what they do
ghelp = discord.Embed(color = 0x7289da)
ghelp.set_author(name = 'Commands/Help', icon_url = '')
ghelp.add_field(name= 'helpme', value = 'This command took you here!', inline = False)
ghelp.add_field(name= 'version', value = 'Displays the current version number and recent updates.', inline = False)
ghelp.add_field(name= 'giveaway', value = '__Can only be accessed by users with the "Giveaway Host" role.__\nStarts a giveaway for the server! This command will ask the host 3 questions. The host will have 30 seconds per question to answer or they will be timed out!', inline = False)
ghelp.add_field(name= 'reroll `#channel_name` `message id`', value = '__Can only be accessed by users with the "Giveaway Host" role.__\nThey must follow the command with the copied message id from the giveaway.', inline = False)
ghelp.set_footer(text = 'Use the prefix "g!" before all commands!')
await ctx.send(embed = ghelp)
@client.command()
@commands.has_role("Giveaway Host")
async def giveaway(ctx):
# Giveaway command requires the user to have a "Giveaway Host" role to function properly
# Stores the questions that the bot will ask the user to answer in the channel that the command was made
# Stores the answers for those questions in a different list
giveaway_questions = ['Which channel will I host the giveaway in?', 'What is the prize?', 'How long should the giveaway run for (in seconds)?',]
giveaway_answers = []
# Checking to be sure the author is the one who answered and in which channel
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
# Askes the questions from the giveaway_questions list 1 by 1
# Times out if the host doesn't answer within 30 seconds
for question in giveaway_questions:
await ctx.send(question)
try:
message = await client.wait_for('message', timeout= 30.0, check= check)
except asyncio.TimeoutError:
await ctx.send('You didn\'t answer in time. Please try again and be sure to send your answer within 30 seconds of the question.')
return
else:
giveaway_answers.append(message.content)
# Grabbing the channel id from the giveaway_questions list and formatting is properly
# Displays an exception message if the host fails to mention the channel correctly
try:
c_id = int(giveaway_answers[0][2:-1])
except:
await ctx.send(f'You failed to mention the channel correctly. Please do it like this: {ctx.channel.mention}')
return
# Storing the variables needed to run the rest of the commands
channel = client.get_channel(c_id)
prize = str(giveaway_answers[1])
time = int(giveaway_answers[2])
# Sends a message to let the host know that the giveaway was started properly
await ctx.send(f'The giveaway for {prize} will begin shortly.\nPlease direct your attention to {channel.mention}, this giveaway will end in {time} seconds.')
# Giveaway embed message
give = discord.Embed(color = 0x2ecc71)
give.set_author(name = f'GIVEAWAY TIME!', icon_url = 'https://i.imgur.com/VaX0pfM.png')
give.add_field(name= f'{ctx.author.name} is giving away: {prize}!', value = f'React with 🎉 to enter!\n Ends in {round(time/60, 2)} minutes!', inline = False)
end = datetime.datetime.utcnow() + datetime.timedelta(seconds=time)
give.set_footer(text = f'Giveaway ends at {end.strftime("%m/%d/%Y, %H:%M")} UTC!')
my_message = await channel.send(embed = give)
# Reacts to the message
await my_message.add_reaction("🎉")
await asyncio.sleep(time)
new_message = await channel.fetch_message(my_message.id)
# Picks a winner
users = await new_message.reactions[0].users().flatten()
users.pop(users.index(client.user))
winner = random.choice(users)
# Announces the winner
winning_announcement = discord.Embed(color = 0xff2424)
winning_announcement.set_author(name = f'THE GIVEAWAY HAS ENDED!', icon_url= 'https://i.imgur.com/DDric14.png')
winning_announcement.add_field(name = f'🎉 Prize: {prize}', value = f'🥳 **Winner**: {winner.mention}\n 🎫 **Number of Entrants**: {len(users)}', inline = False)
winning_announcement.set_footer(text = 'Thanks for entering!')
await channel.send(embed = winning_announcement)
@client.command()
@commands.has_role("Giveaway Host")
async def reroll(ctx, channel: discord.TextChannel, id_ : int):
# Reroll command requires the user to have a "Giveaway Host" role to function properly
try:
new_message = await channel.fetch_message(id_)
except:
await ctx.send("Incorrect id.")
return
# Picks a new winner
users = await new_message.reactions[0].users().flatten()
users.pop(users.index(client.user))
winner = random.choice(users)
# Announces the new winner to the server
reroll_announcement = discord.Embed(color = 0xff2424)
reroll_announcement.set_author(name = f'The giveaway was re-rolled by the host!', icon_url = 'https://i.imgur.com/DDric14.png')
reroll_announcement.add_field(name = f'🥳 New Winner:', value = f'{winner.mention}', inline = False)
await channel.send(embed = reroll_announcement)
# Bot token
client.run(os.environ.get('giveawaybot_pass'))