From 1c3d7e86c200b7e4b1265ef3f39170cf4b2406a4 Mon Sep 17 00:00:00 2001 From: pennacap <64080101+pennacap@users.noreply.github.com> Date: Fri, 11 Mar 2022 09:50:39 +0530 Subject: [PATCH 1/4] Some changes --- cogs/info.py | 5 +++- cogs/update.py | 3 +++ main.py | 65 +++++++++++++++++++++++++------------------------- 3 files changed, 40 insertions(+), 33 deletions(-) diff --git a/cogs/info.py b/cogs/info.py index 259168c..4f08cb5 100644 --- a/cogs/info.py +++ b/cogs/info.py @@ -8,6 +8,7 @@ def __init__(self, bot): self.bot = bot @commands.command(name="faq") + @commands.has_role(608714147378626570) async def faq(self,ctx): role=ctx.guild.get_role(608714147378626570) if role not in ctx.author.roles: @@ -32,6 +33,7 @@ async def faq(self,ctx): @commands.command(name="bots") + @commands.has_role(608714147378626570) async def bots(self,ctx): role=ctx.guild.get_role(608714147378626570) if role not in ctx.author.roles: @@ -56,12 +58,13 @@ async def bots(self,ctx): @commands.command(name="rules") + @commands.has_role(608714147378626570) async def rules(self,ctx): role=ctx.guild.get_role(608714147378626570) if role not in ctx.author.roles: return desc=""" -Before you do anything, we strongly suggest you familirize yourself with the rules and information listed below. +Before you do anything, we strongly suggest you familiarize yourself with the rules and information listed below. **Website:** [discordlabs.org](https://discordlabs.org) Bot List: [bots.discordlabs.org](https://bots.discordlabs.org) diff --git a/cogs/update.py b/cogs/update.py index 2a3fb2e..b1554c5 100644 --- a/cogs/update.py +++ b/cogs/update.py @@ -51,6 +51,7 @@ async def botStatusUpdate(self): offline=0 kicked=0 olist="" + #The start of pain (WHO CODES LIKE THIS) for bot in mydoc: try: b = None @@ -236,6 +237,8 @@ async def botStatusUpdate(self): embed=discord.Embed(title="Data Update Ended",description=F"Total Bots: {len(mydoc)}\n\nOnline Bots: {online}\nAway Bots: {away}\nDND Bots: {dnd}\nKicked Bots: {kicked}\n\n**Offline Bots:** {offline}\nOffline Bots: {olist}",color=self.bot.embed) await m.edit(embed=embed) await asyncio.sleep(30) # task runs every .5 mins + # The fact that someone wrote this is sad. + #print("Stats Update Done") #print(F"Total Bots: {len(mydoc)}\n\nOnline Bots: {online}\nAway Bots: {away}\nDND Bots: {dnd}\nKicked Bots: {kicked}\n\n**Offline Bots:** {offline}\nOffline Bots: {olist}") except: diff --git a/main.py b/main.py index 1cdeb19..a7e74f0 100644 --- a/main.py +++ b/main.py @@ -3,19 +3,12 @@ from pymongo import MongoClient as mcl import os import sys - -TOKEN = "TOKEN" - -cogs = [ - 'cogs.api', - 'cogs.info', - 'cogs.ticket', - 'cogs.update' -] +from dotenv import load_dotenv +load_dotenv() class Bot(commands.AutoShardedBot): def __init__(self): - super().__init__(command_prefix="?", case_insensitve=True) + super().__init__(intents=discord.Intents.all(),command_prefix=commands.when_mentioned_or('!'),help_command=None,activity=discord.Activity(name=' Over Discord Labs.', type=discord.ActivityType.watching),case_insensitive=True) self.check = ":white_check_mark:" self.x = ":x:" @@ -23,15 +16,23 @@ def __init__(self): 365958975201738764 # Anish ] + + async def on_command_error(self,ctx, error): + if ctx.command.has_error_handler(): + return + if isinstance(error,commands.MissingPermissions): + ctx.reply(error.message) + elif isinstance(error,commands.CheckAnyFailure): + ctx.reply(error.message) + elif isinstance(error,commands.MissingRole): + ctx.reply(error.message) + else: + ctx.reply("An error occured") async def on_ready(self): - print(f"Bot is online!") - - activity = discord.Activity(name=' Over Discord Labs.', type=discord.ActivityType.watching) - await bot.change_presence(activity=activity) - print('Intilized successfully.') + print('Bot Started') #MongoDB Stuff - self.client = mcl('MONGODB CONNECTION URL') + self.client = mcl(os.getenv('MONGO_CONNECTION_URL')) self.db=self.client['main'] self.b = self.db['bots'] self.a = self.db['ads'] @@ -67,31 +68,31 @@ async def on_ready(self): - for cog in cogs: - try: - bot.load_extension(cog) - print(f"Loaded {cog}") - except Exception as e: - print(e) - + for file in os.listdir('cogs'): + if file.endswith('.py'): + try: + print(f"Loading {file}") + self.load_extension(f'cogs.{file[:-3]}') + except Exception as e: + print(e,file=sys.error) bot = Bot() -bot.remove_command('help') + + @bot.command() +@commands.check(lambda x: x.id in bot.devs) async def restart(ctx): - if not ctx.author.id in bot.devs: - return await ctx.send(F"Restarting. This may take a little! Please be patient with me.") - await bot.change_presence(status = discord.Status.dnd, activity = discord.Game("RESTARTING BOT!")) - os.execv(sys.executable, ['python3.6'] + sys.argv) + os.execv(sys.executable, [sys.executable]+sys.argv) @bot.command() +@commands.check(lambda x: x.id in bot.devs) async def reload(ctx, cog = None): - if not ctx.author.id in bot.devs: - return if not cog: - return + for cg in bot.cogs.keys(): + bot.reload_extension(cg) + try: bot.reload_extension(cog) await ctx.send(F"{bot.check} Successfully reloaded **{cog}**!") except Exception as e: await ctx.send(f"{bot.x} Oh no! There was an error reloading **{cog}**!\n**({e})**") -bot.run(TOKEN) +bot.run(os.getenv("TOKEN")) From f1001549c54227d6fafc63e12589e93ac68341d3 Mon Sep 17 00:00:00 2001 From: pennacap <64080101+pennacap@users.noreply.github.com> Date: Fri, 11 Mar 2022 09:55:12 +0530 Subject: [PATCH 2/4] Update README.md --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 484cecc..ffa2e61 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,18 @@ The official source code for the Lab Manager Discord Bot. ## Config -You should set your `Discord Token`, `MongoDB` connection URL and any applicable channel ID's in the `main.py` file. There are also other optional configurations in almost all other files. In the case that you want to use the statuspage.io integration, make sure to set your `API token`, `page id` & `metric id` in the `cogs/api.py` file. +Create a file called `.env` in the main directory +Then add the following to the file +``` +TOKEN: (your token) +MONGO_CONNECTION_URL: (the url) +## Optional +STATUS_API_TOKEN: (the statuspage.io API token) +PAGE_ID: (the page ID) +METRIC_ID: (the metric ID) +``` +You should also change any applicable channel ID's in the `main.py` file. There are also other optional configurations in almost all other files. +The last three lines are optional for a statuspage.io integration ## Setup ```shell From 4109a4615c3592df5d6c16a8904caebe294dcfe8 Mon Sep 17 00:00:00 2001 From: pennacap <64080101+pennacap@users.noreply.github.com> Date: Fri, 11 Mar 2022 10:02:25 +0530 Subject: [PATCH 3/4] Some more changes --- .gitignore | 2 ++ README.md | 5 +++-- cogs/api.py | 15 +++++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a62e49 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +*.pyc diff --git a/README.md b/README.md index ffa2e61..d5b326d 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,11 @@ MONGO_CONNECTION_URL: (the url) ## Optional STATUS_API_TOKEN: (the statuspage.io API token) PAGE_ID: (the page ID) -METRIC_ID: (the metric ID) +SMETRIC_ID: (the status metric ID) +BMETRIC_ID: (the bot metric ID) ``` You should also change any applicable channel ID's in the `main.py` file. There are also other optional configurations in almost all other files. -The last three lines are optional for a statuspage.io integration +The last four lines are optional for a statuspage.io integration ## Setup ```shell diff --git a/cogs/api.py b/cogs/api.py index 2448f25..2526db3 100644 --- a/cogs/api.py +++ b/cogs/api.py @@ -1,17 +1,20 @@ from discord.ext import commands, tasks import http.client, urllib.request, urllib.parse, urllib.error, time, requests - -api_key = '' -page_id = '' -smetric_id = '' -bmetric_id = '' +import os +api_key = os.getenv('STATUS_API_TOKEN') +page_id = os.getenv('PAGE_ID') +smetric_id = os.getenv('SMETRIC_ID') +bmetric_id = os.getenv('BMETRIC_ID') api_base = 'api.statuspage.io' class API(commands.Cog): def __init__(self, bot): """Initilize. This function posts latency to the statuspage.io API.""" self.bot = bot - self.postapi.start() + if api_key and page_id and smetric_id and bmetric_id: + self.postapi.start() + else: + print("statuspage.io not configured! Not using the API") @tasks.loop(minutes=1) async def postapi(self): From 59b095c61b9466c4685a87e4c6d61729ca6f5fbf Mon Sep 17 00:00:00 2001 From: pennacap <64080101+pennacap@users.noreply.github.com> Date: Fri, 11 Mar 2022 10:14:30 +0530 Subject: [PATCH 4/4] Update main.py --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index a7e74f0..83abd8e 100644 --- a/main.py +++ b/main.py @@ -79,12 +79,12 @@ async def on_ready(self): @bot.command() -@commands.check(lambda x: x.id in bot.devs) +@commands.check(lambda x: x.author.id in bot.devs) async def restart(ctx): await ctx.send(F"Restarting. This may take a little! Please be patient with me.") os.execv(sys.executable, [sys.executable]+sys.argv) @bot.command() -@commands.check(lambda x: x.id in bot.devs) +@commands.check(lambda x: x.author.id in bot.devs) async def reload(ctx, cog = None): if not cog: for cg in bot.cogs.keys():