├── LICENSE ├── README.md ├── cogs ├── giveaways.json └── giveaways.py ├── config.json ├── main.py └── requirements.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Cursorr 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GiveawayBot (OUTDATED, POOR CODE) 2 | A very advanced giveaway discord bot made with discord.py library. Even if the bot restars or gets down, the giveaways will be ended when the bot is online again. you can create several giveaways simultaneously without any problems. 3 | -------------------------------------------------------------------------------- /cogs/giveaways.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /cogs/giveaways.py: -------------------------------------------------------------------------------- 1 | import discord 2 | import json 3 | import asyncio 4 | import datetime 5 | import time 6 | import random 7 | 8 | from discord.ext import commands, tasks 9 | 10 | 11 | def convert(date): 12 | pos = ["s", "m", "h", "d"] 13 | time_dic = {"s": 1, "m": 60, "h": 3600, "d": 3600 *24} 14 | i = {"s": "Secondes", "m": "Minutes", "h": "Heures", "d": "Jours"} 15 | unit = date[-1] 16 | if unit not in pos: 17 | return -1 18 | try: 19 | val = int(date[:-1]) 20 | 21 | except: 22 | return -2 23 | 24 | if val == 1: 25 | return val * time_dic[unit], i[unit][:-1] 26 | else: 27 | return val * time_dic[unit], i[unit] 28 | 29 | 30 | async def stop_giveaway(self, g_id, data): 31 | channel = self.bot.get_channel(data["channel_id"]) 32 | giveaway_message = await channel.fetch_message(int(g_id)) 33 | users = await giveaway_message.reactions[0].users().flatten() 34 | users.pop(users.index(self.bot.user)) 35 | if len(users) < data["winners"]: 36 | winners_number = len(users) 37 | else: 38 | winners_number = data["winners"] 39 | 40 | winners = random.sample(users, winners_number) 41 | users_mention = [] 42 | for user in winners: 43 | users_mention.append(user.mention) 44 | result_embed = discord.Embed( 45 | title="🎉 {} 🎉".format(data["prize"]), 46 | color=self.color, 47 | description="Congratulations {} you won the giveaway !".format(", ".join(users_mention)) 48 | ) \ 49 | .set_footer(icon_url=self.bot.user.avatar_url, text="Giveaway Ended !") 50 | await giveaway_message.edit(embed=result_embed) 51 | ghost_ping = await channel.send(", ".join(users_mention)) 52 | await ghost_ping.delete() 53 | giveaways = json.load(open("cogs/giveaways.json", "r")) 54 | del giveaways[g_id] 55 | json.dump(giveaways, open("cogs/giveaways.json", "w"), indent=4) 56 | 57 | 58 | 59 | class Giveaways(commands.Cog): 60 | def __init__(self, bot): 61 | self.bot = bot 62 | self.config = json.load(open("config.json", "r")) 63 | self.color = int(self.config["color"], 16) + 0x200 64 | self.giveaway_task.start() 65 | 66 | def cog_unload(self): 67 | self.giveaway_task.cancel() 68 | 69 | @tasks.loop(seconds=5) 70 | async def giveaway_task(self): 71 | await self.bot.wait_until_ready() 72 | giveaways = json.load(open("cogs/giveaways.json", "r")) 73 | 74 | if len(giveaways) == 0: 75 | return 76 | 77 | for giveaway in giveaways: 78 | data = giveaways[giveaway] 79 | if int(time.time()) > data["end_time"]: 80 | await stop_giveaway(self, giveaway, data) 81 | 82 | 83 | @commands.command( 84 | name="giveaway", 85 | aliases=["gstart"] 86 | ) 87 | @commands.has_permissions(manage_guild=True) 88 | async def giveaway(self, ctx: commands.Context): 89 | init = await ctx.send(embed=discord.Embed( 90 | title="🎉 New Giveaway ! 🎉", 91 | description="Please answer the following questions to finalize the creation of the Giveaway", 92 | color=self.color) 93 | .set_footer(icon_url=self.bot.user.avatar_url, text=self.bot.user.name)) 94 | 95 | questions = [ 96 | "What would be the prize of the giveaway?", 97 | "What would the giveaway channel be like? (Please mention the giveaway channel)", 98 | "What would be the duration of the giveaway ? Example: (1d | 1h | 1m | 1s)", 99 | "How many winners do you want for this Giveaway ?" 100 | ] 101 | 102 | def check(message): 103 | return message.author == ctx.author and message.channel == ctx.channel 104 | 105 | index = 1 106 | answers = [] 107 | question_message = None 108 | for question in questions: 109 | embed = discord.Embed( 110 | title="Giveaway 🎉", 111 | description=question, 112 | color=self.color 113 | ).set_footer(icon_url=self.bot.user.avatar_url, text="Giveaway !") 114 | if index == 1: 115 | question_message = await ctx.send(embed=embed) 116 | else: 117 | await question_message.edit(embed=embed) 118 | 119 | try: 120 | user_response = await self.bot.wait_for("message", timeout=120, check=check) 121 | await user_response.delete() 122 | except asyncio.TimeoutError: 123 | await ctx.send(embed=discord.Embed( 124 | title="Error", 125 | color=self.color, 126 | description="You took too long to answer this question" 127 | )) 128 | return 129 | else: 130 | answers.append(user_response.content) 131 | index += 1 132 | try: 133 | channel_id = int(answers[1][2:-1]) 134 | except ValueError: 135 | await ctx.send("You didn't mention the channel correctly, do it like {}.".format(ctx.channel.mention)) 136 | return 137 | 138 | try: 139 | winners = abs(int(answers[3])) 140 | if winners == 0: 141 | await ctx.send("You did not enter an postive number.") 142 | return 143 | except ValueError: 144 | await ctx.send("You did not enter an integer.") 145 | return 146 | prize = answers[0].title() 147 | channel = self.bot.get_channel(channel_id) 148 | converted_time = convert(answers[2]) 149 | if converted_time == -1: 150 | await ctx.send("You did not enter the correct unit of time (s|m|d|h)") 151 | elif converted_time == -2: 152 | await ctx.send("Your time value should be an integer.") 153 | return 154 | await init.delete() 155 | await question_message.delete() 156 | giveaway_embed = discord.Embed( 157 | title="🎉 {} 🎉".format(prize), 158 | color=self.color, 159 | description=f'» **{winners}** {"winner" if winners == 1 else "winners"}\n' 160 | f'» Hosted by {ctx.author.mention}\n\n' 161 | f'» **React with 🎉 to get into the giveaway.**\n' 162 | )\ 163 | .set_footer(icon_url=self.bot.user.avatar_url, text="Ends at") 164 | 165 | giveaway_embed.timestamp = datetime.datetime.utcnow() + datetime.timedelta(seconds=converted_time[0]) 166 | giveaway_message = await channel.send(embed=giveaway_embed) 167 | await giveaway_message.add_reaction("🎉") 168 | now = int(time.time()) 169 | giveaways = json.load(open("cogs/giveaways.json", "r")) 170 | data = { 171 | "prize": prize, 172 | "host": ctx.author.id, 173 | "winners": winners, 174 | "end_time": now + converted_time[0], 175 | "channel_id": channel.id 176 | } 177 | giveaways[str(giveaway_message.id)] = data 178 | json.dump(giveaways, open("cogs/giveaways.json", "w"), indent=4) 179 | 180 | @commands.command( 181 | name="gstop", 182 | aliases=["stop"], 183 | usage="{giveaway_id}" 184 | ) 185 | @commands.has_permissions(manage_guild=True) 186 | async def gstop(self, ctx: commands.Context, message_id): 187 | await ctx.message.delete() 188 | giveaways = json.load(open("cogs/giveaways.json", "r")) 189 | if not message_id in giveaways.keys(): return await ctx.send( 190 | embed=discord.Embed(title="Error", 191 | description="This giveaway ID is not found.", 192 | color=self.color)) 193 | await stop_giveaway(self, message_id, giveaways[message_id]) 194 | 195 | @commands.Cog.listener() 196 | async def on_command_error(self, ctx: commands.Context, error): 197 | if isinstance(error, (commands.CommandNotFound, discord.HTTPException)): 198 | return 199 | 200 | if isinstance(error, commands.MissingPermissions): 201 | return await ctx.send(embed=discord.Embed( 202 | title="Error", 203 | description="You don't have the permission to use this command.", 204 | color=self.color)) 205 | if isinstance(error, commands.MissingRequiredArgument): 206 | return await ctx.send(embed=discord.Embed( 207 | title="Error", 208 | description=f"You forgot to provide an argument, please do it like: `{ctx.command.name} {ctx.command.usage}`", 209 | color=self.color)) 210 | 211 | 212 | def setup(bot): 213 | bot.add_cog(Giveaways(bot)) 214 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "token": "", 3 | "prefix": "!", 4 | "color": "0xad13eb" 5 | } -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import discord 2 | import json 3 | 4 | from discord.ext import commands, tasks 5 | 6 | EXTENTIONS = [ 7 | "cogs.giveaways" 8 | ] 9 | 10 | 11 | class GiveawayBot(commands.Bot): 12 | def __init__(self): 13 | 14 | self.config = json.load(open("config.json", "r")) 15 | 16 | super().__init__(command_prefix=self.config["prefix"], intents=discord.Intents.all()) 17 | 18 | for extention in EXTENTIONS: 19 | try: 20 | self.load_extension(extention) 21 | print(f"{extention} loaded.") 22 | 23 | except Exception as exc: 24 | print("Failed to load extention {}.".format(exc, type(exc).__name__)) 25 | 26 | async def on_ready(self): 27 | print("{} is connected.".format(self.user.display_name)) 28 | self.change_status.start() 29 | 30 | def run(self): 31 | super().run(self.config["token"]) 32 | 33 | @tasks.loop(seconds=5) 34 | async def change_status(self): 35 | giveaways = len(json.load(open("cogs/giveaways.json", "r"))) 36 | await super().change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="{} Giveaways.".format(giveaways))) 37 | 38 | 39 | if __name__ == '__main__': 40 | new_bot = GiveawayBot() 41 | new_bot.run() 42 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | discord.py==1.7.1 2 | --------------------------------------------------------------------------------