From 4712fdb12761fef4d62a0a93c6824768a1be96ed Mon Sep 17 00:00:00 2001 From: Nemika <33802693+Nemijus@users.noreply.github.com> Date: Mon, 8 Mar 2021 12:03:35 +0200 Subject: [PATCH] Added Bump --- cogs/bump.py | 99 ++++++++++++++++++++++++++++++++++++++++++++++ cogs/handler.py | 4 +- cogs/setup.py | 12 +++++- data/settings.json | 3 ++ main.py | 8 ++-- 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 cogs/bump.py create mode 100644 data/settings.json diff --git a/cogs/bump.py b/cogs/bump.py new file mode 100644 index 0000000..648c9b5 --- /dev/null +++ b/cogs/bump.py @@ -0,0 +1,99 @@ +import discord, io, traceback, json, os + +from colorama import Fore, Style, init + +init(autoreset=True) + +from core.database import Servers +from core.files import Data +from core.embeds import Embeds + +commands = discord.ext.commands + +settings = Data("settings").json_read() + +class Bumps(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.config = Data("config").yaml_read() + + @commands.guild_only() + @commands.cooldown(1, settings['cooldown'], commands.BucketType.guild) + @commands.command() + async def bump(self, ctx): + server = Servers(ctx.guild.id) + + if not server.get(): + ctx.command.reset_cooldown(ctx) + return await ctx.send(embed=Embeds(f"You must setup this server first! Use `{self.config['prefix']}setup` to do so!")) + + servers = Servers().get_all() + + success, fail = 0, 0 + + for entry in servers: + try: + server = self.bot.get_guild(entry['_id']) + webhook = await self.bot.fetch_webhook(entry['webhook']) + invite_channel = self.bot.get_channel(entry['invite']) + + invite = await invite_channel.create_invite(max_uses=0, max_age=0, unique=False) + + embed = discord.Embed( + title=server.name, + description=entry['description'], + color=discord.Color(value=entry['color']), + url=invite.url + ) + + embed.add_field(name="šŸŒ Members", value=len(server.members)) + embed.add_field(name="🤣 Emojis", value=f"{len(server.emojis)}/{server.emoji_limit}") + embed.add_field(name="šŸ’Ž Boost Tier", value=f"Tier {server.premium_tier} ({server.premium_subscription_count} Boosts)") + embed.add_field(name="šŸ‘‘ Owner", value=str(server.owner)) + embed.add_field(name="šŸ”— Invite", value=f"[Click to join!]({invite.url})") + embed.set_thumbnail(url=server.icon_url_as(static_format="png")) + embed.set_footer(text=f"Powered by • {self.config['bot_name']}") + + + await webhook.send( + username=self.config['bot_name'], + avatar_url=self.bot.user.avatar_url, + embed=embed + ) + + success += 1 + except Exception as e: + error = f"{e.__class__.__name__}" + value = io.StringIO().getvalue() + print(f"{Fore.RED}[ERROR] {Style.RESET_ALL}{error}\n{Fore.YELLOW}Error was recorded in {Fore.RED}error.log") + #with open("error.log", "w+") as f: + #f.write(f"{value}{traceback.format_exc()}") + + """ + with open("cache_data.json", "w+") as f: + json.dump(entry, f, indent=4) + """ + Servers(entry['_id']).delete() + + try: + await self.bot.get_guild(entry['_id']).owner.send(embed=discord.Embed( + title="āš ļø Server Removed āš ļø", + description="Your server was removed from the database because it caused an error! Make sure I have permission to `Manage Webhooks` and `Create Instant Invites`! I've attached your server info below.", + color=discord.Color.red() + )) + + except: pass + + fail += 1 + + #os.remove("cache_data.json") + + return await ctx.send(embed=discord.Embed( + title="ā« Server Bumped", + description=f"Your server was bumped to `{success+fail}` servers!\nāœ… There were `{success}` successful bumps!\nāŽ There were `{fail}` failed ones, they got booted from the Database!", + color=discord.Color.green() + ) + .set_footer(text=f"Powered by • {self.config['bot_name']}")) + +def setup(bot): + bot.add_cog(Bumps(bot)) \ No newline at end of file diff --git a/cogs/handler.py b/cogs/handler.py index 25cd53d..3064736 100644 --- a/cogs/handler.py +++ b/cogs/handler.py @@ -1,4 +1,6 @@ import discord + +from humanfriendly import format_timespan as ftime from core import embeds commands = discord.ext.commands @@ -17,7 +19,7 @@ class ErrorHandler(commands.Cog): return await ctx.send(embed=embeds.Embeds("You are not allowed to do this.").error()) elif isinstance(error, commands.CommandOnCooldown): seconds = error.retry_after - return await ctx.send(embed=embeds.Embeds(f"**You are on cooldown!** You can use this command again in **{seconds} seconds**.").error()) + return await ctx.send(embed=embeds.Embeds(f"**You are on cooldown!** You can use this command again in **{ftime(seconds)}**.").error()) else: await ctx.send(embed=embeds.Embeds("There was an error executing this command.").error(Error=error)) raise error diff --git a/cogs/setup.py b/cogs/setup.py index e37d378..b435796 100644 --- a/cogs/setup.py +++ b/cogs/setup.py @@ -94,7 +94,7 @@ class BumpSetup(commands.Cog): webhook = await listing.create_webhook(name=self.config['bot_name']) - Servers(ctx.guild.id).add(webhook=webhook.id, listing=listing.id, color=color, description=description) + Servers(ctx.guild.id).add(webhook=webhook.id, invite=invite.id, color=color, description=description) await ctx.send("Setup complete! Server added to DB and the webhook was created.") @@ -152,9 +152,19 @@ class BumpSetup(commands.Cog): check=lambda r,u: r.emoji == "ā™»ļø" and r.message.id == del_message.id and u.id == ctx.author.id ) except asyncio.TimeoutError: + try: + wh = await self.bot.fetch_webhook(cache_data['webhook']) + await wh.delete() + except: + pass return await del_message.remove_reaction("ā™»ļø", self.bot.user) if Servers(ctx.guild.id).get(): + try: + wh = await self.bot.fetch_webhook(cache_data['webhook']) + await wh.delete() + except: + pass return await ctx.send(embed=discord.Embed( title="āŽ Restore Failed", description="The server seems to have been setup from the beginning, therefore restore is not possible.", diff --git a/data/settings.json b/data/settings.json new file mode 100644 index 0000000..aedac3a --- /dev/null +++ b/data/settings.json @@ -0,0 +1,3 @@ +{ + "cooldown": 30 +} \ No newline at end of file diff --git a/main.py b/main.py index d835d0f..66e5910 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,9 @@ init(autoreset=True) config = Data("config").yaml_read() -bot = commands.Bot(command_prefix=config["prefix"], case_insensitive=True, help_command=None, intents=discord.Intents.default()) +intents = discord.Intents.default(); intents.members = True + +bot = commands.Bot(command_prefix=config["prefix"], case_insensitive=True, help_command=None, intents=intents) @bot.event async def on_ready(): @@ -106,7 +108,7 @@ for file in [i for i in os.listdir("cogs") if i.endswith(".py")]: bot.load_extension(f"cogs.{file[:-3]}") print(f"{Fore.GREEN}[SUCCESS] {Style.RESET_ALL}Successfully loaded {Fore.YELLOW}{file}") except Exception as e: - print(f"{Fore.RED}[ERROR] {Style.RESET_ALL}Failed to load {Fore.YELLOW}{file} due to an exception: {Style.DIM}{e}") + print(f"{Fore.RED}[ERROR] {Style.RESET_ALL}Failed to load {Fore.YELLOW}{file} {Style.RESET_ALL}due to an exception: {Style.DIM}{e}") dirs = [i for i in [x for x in os.walk("cogs")][0][1] if i.find(".") == -1] @@ -117,6 +119,6 @@ for folder in dirs: bot.load_extension(f"cogs.{folder}.{file[:-3]}") print(f"{Fore.GREEN}[SUCCESS] {Style.RESET_ALL}Successfully loaded {Fore.YELLOW}{file}") except Exception as e: - print(f"{Fore.RED}[ERROR] {Style.RESET_ALL}Failed to load {Fore.YELLOW}{file} due to an exception: {Style.DIM}{e}") + print(f"{Fore.RED}[ERROR] {Style.RESET_ALL}Failed to load {Fore.YELLOW}{file} {Style.RESET_ALL}due to an exception: {Style.DIM}{e}") bot.run(config["token"])