From 6188339f82589eb2c03620f21455e23c2ddc7c29 Mon Sep 17 00:00:00 2001 From: Jason H <55156874+accountrev@users.noreply.github.com> Date: Wed, 25 Oct 2023 18:41:38 -0400 Subject: [PATCH] Push --- .env.example | 3 +++ .gitignore | 4 +++- bot.py | 32 +++++++++++++++++++------------- utils/color.py | 2 +- utils/internship.py | 7 ++++++- 5 files changed, 32 insertions(+), 16 deletions(-) diff --git a/.env.example b/.env.example index 75ebdb4..95825de 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,5 @@ BOT_TOKEN=YOUR_TOKEN BOT_GUILD_ID=YOUR_GUILD_ID +CHANNEL_ID=YOUR_CHANNEL_ID +TESTING_CHANNEL_ID=YOUR_CHANNEL_ID_FOR_TESTING +TESTING=0_OR_1 \ No newline at end of file diff --git a/.gitignore b/.gitignore index dc46dc1..8d427f2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # BytePSU specific bot/__pycache__/ +database/ +database/internships.json # Byte-compiled / optimized / DLL files __pycache__/ @@ -164,4 +166,4 @@ cython_debug/ # Additional tempCodeRunnerFile.py -database/internships.json + diff --git a/bot.py b/bot.py index 9c1f52e..28c1e58 100644 --- a/bot.py +++ b/bot.py @@ -7,15 +7,11 @@ from discord import app_commands from discord.ext import tasks from dotenv import load_dotenv +load_dotenv() from utils import internship as its from utils.color import calc_avg_color - - -load_dotenv() -TOKEN = os.getenv('BOT_TOKEN') -GUILD_ID = os.getenv('BOT_GUILD_ID') -MY_GUILD = discord.Object(id=GUILD_ID) +its.update_file() class Bot(discord.Client): @@ -25,6 +21,7 @@ def __init__(self): Whenever you work with app cmds, the tree is used to store and work with them.''' self.tree = app_commands.CommandTree(self) self.internships_data = its.open_file() + self.testing = os.getenv('TESTING') == '1' async def setup_hook(self): # This copies the global commands over to the guild/server. @@ -35,10 +32,18 @@ async def setup_hook(self): client = Bot() + +TOKEN = os.getenv('BOT_TOKEN') +GUILD_ID = os.getenv('BOT_GUILD_ID') +MY_GUILD = discord.Object(id=GUILD_ID) +CHANNEL_ID = [os.getenv('CHANNEL_ID') if not client.testing else os.getenv('TESTING_CHANNEL_ID')][0] + + @client.event async def on_ready(): - #await client.get_channel(1160282313859530854).send(f'{client.user} is online and running! - {datetime.now()}') - print(f'{client.user} is online and running! ({datetime.now()})') + + + print(f'{client.user} is online and running! ({datetime.now()}) ({["Testing" if client.testing else "Normal"]})') print('-------------------') update.start() @@ -48,7 +53,7 @@ async def update_account_status(internship_amount: int): await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name=f"{internship_amount} internship{'s' if internship_amount > 1 else ''}")) print(f"Bot status changed to Watching", f"{internship_amount} internship{'s' if internship_amount > 1 else ''}") -def create_internship_embed(index: int): +def create_internship_embed(index: int, user_gen: bool = False): print(f"A new internship embed is being created on {datetime.now()}") if index > len(client.internships_data) - 1 or index < 0: @@ -59,7 +64,7 @@ def create_internship_embed(index: int): print(client.internships_data[index]) - embed = discord.Embed(description=f"<@&1165743852708180049> | *Apply now!*\n{'<:D10:1165807583655891004> '*9}\n**{its.check_for_key(client.internships_data[index], 'title')} • {its.check_for_key(client.internships_data[index], 'season')} {its.check_for_key(client.internships_data[index],'yr')}**\n\n:earth_americas:   :dollar:", + embed = discord.Embed(description=f"{'<@&1165743852708180049> | ' if not user_gen else ''}*Apply now!*\n{'<:D10:1165807583655891004> '*9}\n**{its.check_for_key(client.internships_data[index], 'title')} • {its.check_for_key(client.internships_data[index], 'season')} {its.check_for_key(client.internships_data[index],'yr')}**\n\n:earth_americas:   :dollar:", title=f"{its.check_for_key(client.internships_data[index], 'company')} (#{index})", colour=discord.Colour(int(calc_avg_color(client.internships_data[index]['icon']).lstrip('#'), 16)), timestamp=datetime.now()) @@ -86,7 +91,7 @@ def create_internship_embed(index: int): async def get_internship(interact: discord.Interaction, index: int): print(interact.user, f"asked for internship #{index} on {datetime.now()}") try: - embed, url_view = create_internship_embed(index) + embed, url_view = create_internship_embed(index, True) await interact.response.send_message(embed=embed, view=url_view) except Exception as e: @@ -98,7 +103,7 @@ async def random_internship(interact: discord.Interaction): print(interact.user, f"asked for a random internship on {datetime.now()}") try: random_index = randint(0, len(client.internships_data) - 1) - embed, url_view = create_internship_embed(random_index) + embed, url_view = create_internship_embed(random_index, True) await interact.response.send_message(embed=embed, view=url_view) except Exception as e: @@ -110,7 +115,7 @@ async def random_internship(interact: discord.Interaction): async def update(): print(f"Initiating a new update on {datetime.now()}") - channel_to_post = client.get_channel(1160282313859530854) + channel_to_post = client.get_channel(CHANNEL_ID) try: changes = its.check_for_update() @@ -158,3 +163,4 @@ async def test_refresh_json(interact: discord.Interaction): client.run(TOKEN) + diff --git a/utils/color.py b/utils/color.py index 50ef497..288dabd 100644 --- a/utils/color.py +++ b/utils/color.py @@ -40,4 +40,4 @@ def calc_avg_color(url: str): if __name__ == "__main__": - print("color.py is not meant to be run directly unless for testing.") \ No newline at end of file + print("color.py: color.py is not meant to be run directly unless for testing.") diff --git a/utils/internship.py b/utils/internship.py index ce33bde..b5ad59e 100644 --- a/utils/internship.py +++ b/utils/internship.py @@ -1,5 +1,6 @@ import json import requests +import os def check_for_key(internship, key): try: @@ -30,6 +31,10 @@ def get_internship_file(filter=True): def update_file(): internships = get_internship_file() + if not os.path.exists('database'): + print("internship.py: Database folder missing, creating.") + os.mkdir('database') + with open('database/internships.json', 'w') as f: json.dump(internships, f, indent=4) f.close() @@ -62,4 +67,4 @@ def check_for_update(): } if __name__ == "__main__": - print("internship.py is not meant to be run directly unless for testing.") \ No newline at end of file + print("internship.py: internship.py is not meant to be run directly unless for testing.") \ No newline at end of file