Skip to content

Commit

Permalink
Merge pull request #19 from BytePSU/JH
Browse files Browse the repository at this point in the history
Preparation for host
  • Loading branch information
AdamPSU authored Oct 25, 2023
2 parents 464e9d5 + 6188339 commit 3f59af8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# BytePSU specific
bot/__pycache__/
database/
database/internships.json

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down Expand Up @@ -164,4 +166,4 @@ cython_debug/

# Additional
tempCodeRunnerFile.py
database/internships.json

32 changes: 19 additions & 13 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -158,3 +163,4 @@ async def test_refresh_json(interact: discord.Interaction):


client.run(TOKEN)

2 changes: 1 addition & 1 deletion utils/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
print("color.py: color.py is not meant to be run directly unless for testing.")
7 changes: 6 additions & 1 deletion utils/internship.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import requests
import os

def check_for_key(internship, key):
try:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -62,4 +67,4 @@ def check_for_update():
}

if __name__ == "__main__":
print("internship.py is not meant to be run directly unless for testing.")
print("internship.py: internship.py is not meant to be run directly unless for testing.")

0 comments on commit 3f59af8

Please sign in to comment.