Skip to content

Commit

Permalink
feat/WIT-189: new command (#70)
Browse files Browse the repository at this point in the history
* feat/WIT-189: new command

* feat/WIT-189: new command

* feat/WIT-189: new command

* feat/WIT-189: new command

* feat/WIT-189: new command

* feat/WIT-189: changes made
  • Loading branch information
ash1248 authored May 14, 2024
1 parent b70636e commit 4d7ecb9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
15 changes: 15 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ def get_most_recent_event():
{"content_type": "pastEvents", "limit": 1, "order": "-fields.index"}
)
return past_events[0]


def get_media_resources():
resources = {
"publication": client.entries(
{"content_type": "publications", "order": "fields.index"}
),
"blog": client.entries(
{"content_type": "blogRecommendations", "order": "fields.blogNo"}
),
"podcast": client.entries(
{"content_type": "podcastEpisode", "order": "fields.link"}
),
}
return resources
43 changes: 43 additions & 0 deletions cogs/random_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import discord
from discord import app_commands
from discord.ext import commands
import random

from api import get_media_resources

MEDIA_LINK = "https://unswwit.com/media/"


class RandomResourceCog(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot

@app_commands.command(
name="random-resource", description="Send a random WIT resource!"
)
async def send_random_resource(self, int: discord.Interaction):
link = ""
resources = get_media_resources()
resource_type = random.choice(list(resources.keys()))
resource = random.choice(resources[resource_type]).fields()
resource_title = ""

if resource_type == "publication":
link = f"{MEDIA_LINK}{resource_type}/{resource['index']}"
resource_title = resource["heading"]
elif resource_type == "blog":
link = f"{MEDIA_LINK}{resource_type}/{resource['index']}"
resource_title = resource["title"]
elif resource_type == "podcast":
link = resource["link"]
resource_title = resource["title"]

try:
message = f"**{resource_type.capitalize()} Recommendation\n**Name: {resource_title}\nLink: {link}"
await int.response.send_message(message)
except:
await int.response.send_message("Sorry, no posts yet!")


async def setup(bot: commands.Bot):
await bot.add_cog(RandomResourceCog(bot))

0 comments on commit 4d7ecb9

Please sign in to comment.