diff options
Diffstat (limited to 'bot/seasons')
-rw-r--r-- | bot/seasons/valentines/lovecalculator.py | 71 | ||||
-rw-r--r-- | bot/seasons/valentines/movie_generator.py | 66 |
2 files changed, 137 insertions, 0 deletions
diff --git a/bot/seasons/valentines/lovecalculator.py b/bot/seasons/valentines/lovecalculator.py new file mode 100644 index 00000000..b995aef4 --- /dev/null +++ b/bot/seasons/valentines/lovecalculator.py @@ -0,0 +1,71 @@ +import json +import logging +from pathlib import Path +from random import choice + +import discord +from discord.ext import commands + +from bot.constants import Roles + +log = logging.getLogger(__name__) + +with open(Path("bot", "resources", "valentines", "love_matches.json"), "r") as file: + LOVE_DATA = json.load(file) +LOVE_LEVELS = [int(x) for x in LOVE_DATA] + + +class LoveCalculator: + """ + A cog for calculating the love between two people + """ + def __init__(self, bot): + self.bot = bot + + @commands.command(aliases=('love_calculator', 'love_calc')) + @commands.cooldown(rate=1, per=5.0, type=commands.BucketType.user) + async def love(self, ctx, name_one: discord.Member, name_two: discord.Member = None): + """ + Calculates the love between two given names + + DO NOT SPAM @mentions! There are five ways to hand over a name to this command: + 1. ID of the user + 2. Name + Discriminator of the user (name#discrim) (1) + 3. Username (1) + 4. Nickname (1) + 5. @mention + + Using method 1-4 is highly encouraged, as nobody likes unwanted pings + they'll count as spam. + Skipping the second name will lead to something awesome. + + *(1): If the name has any form of spacing, the name must be wrapped inside quotes. Example: + .love Ves Zappa Niko Laus -> Will not work + .love "Ves Zappa" "Niko Laus" -> Will work + """ + + if name_two is None: + staff = ctx.guild.get_role(Roles.helpers).members + name_two = choice(staff) + + love_meter = (name_one.id + name_two.id) % 101 + love_idx = str(sorted(x for x in LOVE_LEVELS if x <= love_meter)[-1]) + love_status = choice(LOVE_DATA[love_idx]["titles"]) + + embed = discord.Embed( + title=love_status, + description=( + f'{name_one.display_name} \u2764 {name_two.display_name}' + f' scored {love_meter}%!\n\u200b' + ), + color=discord.Color.dark_magenta() + ) + embed.add_field( + name='A letter from Dr. Love:', + value=LOVE_DATA[love_idx]["text"] + ) + + await ctx.message.channel.send(embed=embed) + + +def setup(bot): + bot.add_cog(LoveCalculator(bot)) diff --git a/bot/seasons/valentines/movie_generator.py b/bot/seasons/valentines/movie_generator.py new file mode 100644 index 00000000..b52eba7f --- /dev/null +++ b/bot/seasons/valentines/movie_generator.py @@ -0,0 +1,66 @@ +import logging +import random +from os import environ +from urllib import parse + +import discord +from discord.ext import commands + +TMDB_API_KEY = environ.get("TMDB_API_KEY") + +log = logging.getLogger(__name__) + + +class RomanceMovieFinder: + """ + A cog that returns a random romance movie suggestion to a user + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name="romancemovie") + async def romance_movie(self, ctx): + """ + Randomly selects a romance movie and displays information about it + """ + # selecting a random int to parse it to the page parameter + random_page = random.randint(0, 20) + # TMDB api params + params = { + "api_key": TMDB_API_KEY, + "language": "en-US", + "sort_by": "popularity.desc", + "include_adult": "false", + "include_video": "false", + "page": random_page, + "with_genres": "10749" + } + # the api request url + request_url = "https://api.themoviedb.org/3/discover/movie?" + parse.urlencode(params) + async with self.bot.http_session.get(request_url) as resp: + # trying to load the json file returned from the api + try: + data = await resp.json() + # selecting random result from results object in the json file + selected_movie = random.choice(data["results"]) + + embed = discord.Embed( + title=f":sparkling_heart: {selected_movie['title']} :sparkling_heart:", + description=selected_movie["overview"], + ) + embed.set_image(url=f"http://image.tmdb.org/t/p/w200/{selected_movie['poster_path']}") + embed.add_field(name="Release date :clock1:", value=selected_movie["release_date"]) + embed.add_field(name="Rating :star2:", value=selected_movie["vote_average"]) + await ctx.send(embed=embed) + except KeyError: + warning_message = "A KeyError was raised while fetching information on the movie. The API service" \ + " could be unavailable or the API key could be set incorrectly." + embed = discord.Embed(title=warning_message) + log.warning(warning_message) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(RomanceMovieFinder(bot)) + log.debug("Random romance movie cog loaded!") |