From 1dc7d3859e071b3587e41098ceaff37971317c9a Mon Sep 17 00:00:00 2001 From: xedre Date: Fri, 5 Oct 2018 22:22:20 +0100 Subject: added docstring to repository --- bot/cogs/template.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'bot/cogs') diff --git a/bot/cogs/template.py b/bot/cogs/template.py index 89f12fe1..b3f4da21 100644 --- a/bot/cogs/template.py +++ b/bot/cogs/template.py @@ -12,6 +12,9 @@ class Template: @commands.command(name='repo', aliases=['repository', 'project'], brief='A link to the repository of this bot.') async def repository(self, ctx): + """ + A command to send the hacktoberbot github project + """ await ctx.send('https://github.com/discord-python/hacktoberbot') @commands.group(name='git', invoke_without_command=True) -- cgit v1.2.3 From dec834d055e3694452af8289a05abcf1b57e9a35 Mon Sep 17 00:00:00 2001 From: Ryan Gables Date: Sat, 6 Oct 2018 02:33:19 -0700 Subject: Roughed out omdb requests --- bot/cogs/rand_movie.py | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 bot/cogs/rand_movie.py (limited to 'bot/cogs') diff --git a/bot/cogs/rand_movie.py b/bot/cogs/rand_movie.py new file mode 100644 index 00000000..d531b32b --- /dev/null +++ b/bot/cogs/rand_movie.py @@ -0,0 +1,66 @@ +import requests +import random +from pprint import pprint +from discord.ext import commands + +class Movies: + + """ + TODO. + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='movie', aliases=['rand_movie', 'movie'], brief='Pick a scary movie') + async def repository(self, ctx): + await ctx.send('https://github.com/discord-python/hacktoberbot') + + @staticmethod + def select_movie(api_key): + params = { + 'apikey': api_key, + 'type': 'movie', + 's': 'halloween' + } + + response = requests.get('http://www.omdbapi.com/', params) + + movies = [] + + for movie in response.json().get('Search'): + movie_id = movie.get('imdbID') + movies.append(movie_id) + + selection = random.choice(movies) + + params = { + 'apikey': api_key, + 'i': selection + } + + response = requests.get('http://www.omdbapi.com/', params) + + return response.json() + + + # @commands.group(name='git', invoke_without_command=True) + # async def github(self, ctx): + # """ + # A command group with the name git. You can now create sub-commands such as git commit. + # """ + # + # await ctx.send('Resources to learn **Git**: https://try.github.io/.') + # + # @github.command() + # async def commit(self, ctx): + # """ + # A command that belongs to the git command group. Invoked using git commit. + # """ + # + # await ctx.send('`git commit -m "First commit"` commits tracked changes.') + + +# Required in order to load the cog, use the class name in the add_cog function. +def setup(bot): + bot.add_cog(Template(bot)) -- cgit v1.2.3 From 9af16d77de893245a98ddb7c91825dd54bd6b106 Mon Sep 17 00:00:00 2001 From: Ryan Gables Date: Sat, 6 Oct 2018 18:21:12 -0700 Subject: made apikey env var + formatted omdb response --- bot/cogs/movie.py | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ bot/cogs/rand_movie.py | 66 -------------------------------------------------- 2 files changed, 65 insertions(+), 66 deletions(-) create mode 100644 bot/cogs/movie.py delete mode 100644 bot/cogs/rand_movie.py (limited to 'bot/cogs') diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py new file mode 100644 index 00000000..45714005 --- /dev/null +++ b/bot/cogs/movie.py @@ -0,0 +1,65 @@ +import requests +import random +from discord.ext import commands +from os import environ + +OMDB_API_KEY = environ.get('OMDB_API_KEY') + + +class Movie: + """ + Prints the details of a random scary movie to discord chat + """ + + def __init__(self, bot): + self.bot = bot + + @commands.command(name='movie', aliases=['scary_movie'], brief='Pick me a scary movie') + async def random_movie(self, ctx): + selection = await self.select_movie() + movie_details = await self.format_metadata(selection) + + await ctx.send(movie_details) + + @staticmethod + async def select_movie(): + """ + Selects a random movie and returns a json of movie details from omdb + """ + + # TODO: Come up w/ a scary movie list to select from. Currently returns random Halloween movie + omdb_params = { + 'apikey': OMDB_API_KEY, + 'type': 'movie', + 's': 'halloween' + } + response = requests.get('http://www.omdbapi.com/', omdb_params) + + movies = [] + for movie in response.json().get('Search'): + movie_id = movie.get('imdbID') + movies.append(movie_id) + + selection = random.choice(movies) + + omdb_params = { + 'apikey': OMDB_API_KEY, + 'i': selection + } + response = requests.get('http://www.omdbapi.com/', omdb_params) + + return response.json() + + @staticmethod + async def format_metadata(movie): + """ + Formats raw omdb data to be displayed in discord chat + """ + display_text = f"You should watch {movie.get('Title')} ({movie.get('Year')})\n" \ + f"https://www.imdb.com/title/{movie.get('imdbID')}" + + return display_text + + +def setup(bot): + bot.add_cog(Movie(bot)) diff --git a/bot/cogs/rand_movie.py b/bot/cogs/rand_movie.py deleted file mode 100644 index d531b32b..00000000 --- a/bot/cogs/rand_movie.py +++ /dev/null @@ -1,66 +0,0 @@ -import requests -import random -from pprint import pprint -from discord.ext import commands - -class Movies: - - """ - TODO. - """ - - def __init__(self, bot): - self.bot = bot - - @commands.command(name='movie', aliases=['rand_movie', 'movie'], brief='Pick a scary movie') - async def repository(self, ctx): - await ctx.send('https://github.com/discord-python/hacktoberbot') - - @staticmethod - def select_movie(api_key): - params = { - 'apikey': api_key, - 'type': 'movie', - 's': 'halloween' - } - - response = requests.get('http://www.omdbapi.com/', params) - - movies = [] - - for movie in response.json().get('Search'): - movie_id = movie.get('imdbID') - movies.append(movie_id) - - selection = random.choice(movies) - - params = { - 'apikey': api_key, - 'i': selection - } - - response = requests.get('http://www.omdbapi.com/', params) - - return response.json() - - - # @commands.group(name='git', invoke_without_command=True) - # async def github(self, ctx): - # """ - # A command group with the name git. You can now create sub-commands such as git commit. - # """ - # - # await ctx.send('Resources to learn **Git**: https://try.github.io/.') - # - # @github.command() - # async def commit(self, ctx): - # """ - # A command that belongs to the git command group. Invoked using git commit. - # """ - # - # await ctx.send('`git commit -m "First commit"` commits tracked changes.') - - -# Required in order to load the cog, use the class name in the add_cog function. -def setup(bot): - bot.add_cog(Template(bot)) -- cgit v1.2.3 From eee33d3d294686a99e6410ad9d43ff155ee763dd Mon Sep 17 00:00:00 2001 From: Ryan Gables Date: Tue, 9 Oct 2018 01:16:38 -0700 Subject: Embeds movie data from TMDb --- bot/cogs/movie.py | 88 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 61 insertions(+), 27 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py index 45714005..65c96075 100644 --- a/bot/cogs/movie.py +++ b/bot/cogs/movie.py @@ -1,64 +1,98 @@ import requests import random -from discord.ext import commands from os import environ +from discord.ext import commands +from discord import Embed -OMDB_API_KEY = environ.get('OMDB_API_KEY') +TMDB_API_KEY = environ.get('TMDB_API_KEY') +TMDB_TOKEN = environ.get('TMDB_TOKEN') class Movie: """ - Prints the details of a random scary movie to discord chat + Selects a random scary movie and embeds info into discord chat """ def __init__(self, bot): self.bot = bot - @commands.command(name='movie', aliases=['scary_movie'], brief='Pick me a scary movie') + @commands.command(name='movie', alias=['tmdb'], brief='Pick a scary movie') async def random_movie(self, ctx): selection = await self.select_movie() movie_details = await self.format_metadata(selection) - await ctx.send(movie_details) + await ctx.send(embed=movie_details) @staticmethod async def select_movie(): """ - Selects a random movie and returns a json of movie details from omdb + Selects a random movie and returns a json of movie details from TMDb """ - # TODO: Come up w/ a scary movie list to select from. Currently returns random Halloween movie - omdb_params = { - 'apikey': OMDB_API_KEY, - 'type': 'movie', - 's': 'halloween' + url = 'https://api.themoviedb.org/4/discover/movie' + params = { + 'with_genres': '27', + 'vote_count.gte': '5' + } + headers = { + 'Authorization': 'Bearer ' + TMDB_TOKEN, + 'Content-Type': 'application/json;charset=utf-8' } - response = requests.get('http://www.omdbapi.com/', omdb_params) - movies = [] - for movie in response.json().get('Search'): - movie_id = movie.get('imdbID') - movies.append(movie_id) + # Get total page count of horror movies + response = requests.get(url=url, params=params, headers=headers) + total_pages = response.json().get('total_pages') - selection = random.choice(movies) + # Get movie details from one random result on a random page + params['page'] = random.randint(1, total_pages) + response = requests.get(url=url, params=params, headers=headers) + selection_id = random.choice(response.json().get('results')).get('id') - omdb_params = { - 'apikey': OMDB_API_KEY, - 'i': selection - } - response = requests.get('http://www.omdbapi.com/', omdb_params) + # Get full details and credits + selection = requests.get(url='https://api.themoviedb.org/3/movie/' + str(selection_id), + params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'}) - return response.json() + return selection.json() @staticmethod async def format_metadata(movie): """ - Formats raw omdb data to be displayed in discord chat + Formats raw TMDb data to be embedded in discord chat """ - display_text = f"You should watch {movie.get('Title')} ({movie.get('Year')})\n" \ - f"https://www.imdb.com/title/{movie.get('imdbID')}" - return display_text + tmdb_url = 'https://www.themoviedb.org/movie/' + str(movie.get('id')) + poster = 'https://image.tmdb.org/t/p/original' + movie.get('poster_path') + + cast = [] + for actor in movie.get('credits').get('cast')[:3]: + cast.append(actor.get('name')) + + director = movie.get('credits').get('crew')[0].get('name') + + rating_count = movie.get('vote_average') / 2 + rating = '' + + for i in range(int(rating_count)): + rating += ':skull:' + + if (rating_count % 1) >= .5: + rating += ':bat:' + + embed = Embed( + colour=0x01d277, + title='**' + movie.get('title') + '**', + url=tmdb_url, + description=movie.get('overview') + ) + embed.set_image(url=poster) + embed.add_field(name='Staring', value=', '.join(cast)) + embed.add_field(name='Directed by', value=director) + embed.add_field(name='Year', value=movie.get('release_date')[:4]) + embed.add_field(name='Runtime', value=str(movie.get('runtime')) + 'm') + embed.add_field(name='Spooky Rating', value=rating) + embed.set_footer(text='powered by themoviedb.org') + + return embed def setup(bot): -- cgit v1.2.3 From 9f4065bcc720857564498d526c88bcc0a8c51881 Mon Sep 17 00:00:00 2001 From: Ryan Gables Date: Tue, 9 Oct 2018 01:40:39 -0700 Subject: spelling --- bot/cogs/movie.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py index 65c96075..bb6f8df8 100644 --- a/bot/cogs/movie.py +++ b/bot/cogs/movie.py @@ -85,10 +85,10 @@ class Movie: description=movie.get('overview') ) embed.set_image(url=poster) - embed.add_field(name='Staring', value=', '.join(cast)) + embed.add_field(name='Starring', value=', '.join(cast)) embed.add_field(name='Directed by', value=director) embed.add_field(name='Year', value=movie.get('release_date')[:4]) - embed.add_field(name='Runtime', value=str(movie.get('runtime')) + 'm') + embed.add_field(name='Runtime', value=str(movie.get('runtime')) + ' min') embed.add_field(name='Spooky Rating', value=rating) embed.set_footer(text='powered by themoviedb.org') -- cgit v1.2.3 From f12940261d7afd7254c703d30272fdc688cb8ef7 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Tue, 9 Oct 2018 11:42:09 +0200 Subject: Implement Halloweenify feature. --- bot/cogs/halloweenify.py | 46 ++++++++++++++++++++++++ bot/resources/halloweenify.json | 79 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 bot/cogs/halloweenify.py create mode 100644 bot/resources/halloweenify.json (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py new file mode 100644 index 00000000..0422e787 --- /dev/null +++ b/bot/cogs/halloweenify.py @@ -0,0 +1,46 @@ +from pathlib import Path +from json import load +from random import choice + + +import discord +from discord.ext import commands +from discord.ext.commands.cooldowns import BucketType + + +class Halloweenify: + + """ + A cog to change a invokers nickname to a spooky one! + """ + + def __init__(self, bot): + self.bot = bot + + @commands.cooldown(1, 300, BucketType.user) + @commands.command() + async def halloweenify(self, ctx): + with open(Path('../bot/resources', 'halloweenify.json'), 'r') as f: + data = load(f) + + # Choose a random character from our list we loaded above and set apart the nickname and image url. + character = choice(data['characters']) + nickname = ''.join([nickname for nickname in character]) + image = ''.join([character[nickname] for nickname in character]) + + # Build up a Embed + embed = discord.Embed() + embed.colour = discord.Colour.dark_orange() + embed.title = 'Wow!' + embed.description = ( + f'Your previous nickname, **{ctx.author.display_name}**, wasn\'t spooky enough for you that you have ' + f'decided to change it?! Okay, your new nickname will be **{nickname}**.\n\n' + ) + embed.set_image(url=image) + + await ctx.author.edit(nick=nickname) + await ctx.send(embed=embed) + + +def setup(bot): + bot.add_cog(Halloweenify(bot)) diff --git a/bot/resources/halloweenify.json b/bot/resources/halloweenify.json new file mode 100644 index 00000000..458f9342 --- /dev/null +++ b/bot/resources/halloweenify.json @@ -0,0 +1,79 @@ +{ + "characters": [ + { + "Michael Myers": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f57x2f9353301x2fwzqoqvitx2fuqkpimt-ugmza-x78pwbw-c95x3fex3d438x26yx3d38x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Jason Voorhees": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f42x2f9050161x2fwzqoqvitx2friawv-dwwzpmma-nqtu-kpizikbmza-x78pwbw-c1x3fex3d438x26yx3d38x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Freddy Krueger": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fvwlm_quox2f30x2f9800154x2fwzqoqvitx2fnzmllg-szcmomz-nqtu-kpizikbmza-x78pwbw-9x3fex3d438x26yx3d38x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Pennywise the Dancing Clown": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f436x2f91927608x2fwzqoqvitx2fx78mvvgeqam-bpm-livkqvo-ktwev-nqtu-kpizikbmza-x78pwbw-c0x3fex3d438x26yx3d38x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Charles Lee Ray": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f14x2f581261x2fwzqoqvitx2fkpiztma-tmm-zig-c90x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Leatherface": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f50x2f9204163x2fwzqoqvitx2ftmibpmznikm-c08x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Jack Torrance": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fvwlm_quox2f41x2f9032825x2fwzqoqvitx2friks-bwzzivkm-nqtu-kpizikbmza-x78pwbw-9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Ghostface": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f32x2f9845748x2fwzqoqvitx2fopwabnikm-nqtu-kpizikbmza-x78pwbw-c9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Regan MacNeil": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f73x2f9665843x2fwzqoqvitx2fzmoiv-uikvmqt-zmkwzlqvo-izbqaba-ivl-ozwcx78a-x78pwbw-c1x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Hannibal Lecter": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f35x2f9903368x2fwzqoqvitx2fpivvqjit-tmkbmz-nqtu-kpizikbmza-x78pwbw-c99x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Samara Morgan": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f77x2f9744082x2fwzqoqvitx2faiuizi-uwzoiv-nqtu-kpizikbmza-x78pwbw-c9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Pinhead": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f79x2f9683232x2fwzqoqvitx2fx78qvpmil-nqtu-kpizikbmza-x78pwbw-c4x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Bruce": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f2047x2f63159728x2fwzqoqvitx2friea-c3x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Xenomorph": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f909x2f0297919x2fwzqoqvitx2ffmvwuwzx78p-x78pwbw-c91x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "The Thing": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f1947x2f41148806x2fwzqoqvitx2fbpm-bpqvo-x78pwbw-c6x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Sadako Yamamura": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f76x2f9735755x2fwzqoqvitx2failisw-giuiuczi-c4x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Jigsaw Killer": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fvwlm_quox2f43x2f9065767x2fwzqoqvitx2frqoaie-sqttmz-nqtu-kpizikbmza-x78pwbw-9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Norman Bates": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f63x2f9467317x2fwzqoqvitx2fvwzuiv-jibma-nqtu-kpizikbmza-x78pwbw-c9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Candyman": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f255x2f7305168x2fwzqoqvitx2fkivlguiv-c5x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "The Creeper": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fvwlm_quox2f998x2f0963452x2fwzqoqvitx2fbpm-kzmmx78mz-nqtu-kpizikbmza-x78pwbw-9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Damien Thorn": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f29x2f685981x2fwzqoqvitx2fliuqmv-bpwzv-x78pwbw-c1x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Joker": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f903x2f0276049x2fwzqoqvitx2frwsmz-c71x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Cujo": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f1581x2f52837948x2fwzqoqvitx2fkcrw-nqtu-kpizikbmza-x78pwbw-c9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Gage Creed": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fcamz_vwlm_quox2f1968x2f41364699x2fwzqoqvitx2foiom-kzmml-nqtu-kpizikbmza-x78pwbw-c9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Chatterer": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fvwlm_quox2f14x2f586061x2fwzqoqvitx2fkpibbmzmz-nqtu-kpizikbmza-x78pwbw-9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + } + ] +} \ No newline at end of file -- cgit v1.2.3 From a4c8da298b4b44fe222b304676029407734c77f6 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 10 Oct 2018 11:04:24 +0200 Subject: Change message to a more friendly one. --- bot/cogs/halloweenify.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index 0422e787..58cef019 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -33,8 +33,9 @@ class Halloweenify: embed.colour = discord.Colour.dark_orange() embed.title = 'Wow!' embed.description = ( - f'Your previous nickname, **{ctx.author.display_name}**, wasn\'t spooky enough for you that you have ' - f'decided to change it?! Okay, your new nickname will be **{nickname}**.\n\n' + f'**{ctx.author.display_name}** wasn\'t spooky enough for you? That\'s understandable, ' + f'{ctx.author.display_name} isn\'t scary at all! Let me think of something better. Hmm... I got it!\n Your ' + f'new nickname will be: \n 👻 **{nickname}** 🎃' ) embed.set_image(url=image) -- cgit v1.2.3 From 94a275c3b0222a261aa7d07b8ab37f92f9d82fd3 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 10 Oct 2018 11:18:55 +0200 Subject: Minor changes to the Embed. --- bot/cogs/halloweenify.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index 58cef019..c49e0058 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -31,11 +31,11 @@ class Halloweenify: # Build up a Embed embed = discord.Embed() embed.colour = discord.Colour.dark_orange() - embed.title = 'Wow!' + embed.title = 'Not spooky enough?' embed.description = ( f'**{ctx.author.display_name}** wasn\'t spooky enough for you? That\'s understandable, ' f'{ctx.author.display_name} isn\'t scary at all! Let me think of something better. Hmm... I got it!\n Your ' - f'new nickname will be: \n 👻 **{nickname}** 🎃' + f'new nickname will be: \n :jack_o_lantern: **{nickname}** :ghost:' ) embed.set_image(url=image) -- cgit v1.2.3 From 8ea196581e70e149ce998d9c49bf8bf1f9a746db Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 10 Oct 2018 11:21:50 +0200 Subject: Ooops, reversed the emojis. --- bot/cogs/halloweenify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index c49e0058..226d55db 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -35,7 +35,7 @@ class Halloweenify: embed.description = ( f'**{ctx.author.display_name}** wasn\'t spooky enough for you? That\'s understandable, ' f'{ctx.author.display_name} isn\'t scary at all! Let me think of something better. Hmm... I got it!\n Your ' - f'new nickname will be: \n :jack_o_lantern: **{nickname}** :ghost:' + f'new nickname will be: \n :ghost: **{nickname}** :jack_o_lantern:' ) embed.set_image(url=image) -- cgit v1.2.3 From ff5882981e0fcaba65f8b6ffa0dcbdd756826503 Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 10 Oct 2018 11:26:59 +0200 Subject: Missing newline. --- bot/cogs/halloweenify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index 226d55db..e4fb787c 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -35,7 +35,7 @@ class Halloweenify: embed.description = ( f'**{ctx.author.display_name}** wasn\'t spooky enough for you? That\'s understandable, ' f'{ctx.author.display_name} isn\'t scary at all! Let me think of something better. Hmm... I got it!\n Your ' - f'new nickname will be: \n :ghost: **{nickname}** :jack_o_lantern:' + f'new nickname will be: \n\n :ghost: **{nickname}** :jack_o_lantern:' ) embed.set_image(url=image) -- cgit v1.2.3 From fb46d8f2dba8a5f00bf7914c8b4a026acbbf55fc Mon Sep 17 00:00:00 2001 From: Marko Kovačević Date: Wed, 10 Oct 2018 11:28:46 +0200 Subject: Another cosmetic mistake. --- bot/cogs/halloweenify.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index e4fb787c..8a9db3df 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -34,8 +34,8 @@ class Halloweenify: embed.title = 'Not spooky enough?' embed.description = ( f'**{ctx.author.display_name}** wasn\'t spooky enough for you? That\'s understandable, ' - f'{ctx.author.display_name} isn\'t scary at all! Let me think of something better. Hmm... I got it!\n Your ' - f'new nickname will be: \n\n :ghost: **{nickname}** :jack_o_lantern:' + f'{ctx.author.display_name} isn\'t scary at all! Let me think of something better. Hmm... I got it!\n\n ' + f'Your new nickname will be: \n :ghost: **{nickname}** :jack_o_lantern:' ) embed.set_image(url=image) -- cgit v1.2.3 From 1ce65cf745d409550f36fa774825866c98d36cfe Mon Sep 17 00:00:00 2001 From: hundredrab Date: Thu, 11 Oct 2018 01:40:26 +0530 Subject: Fix #27: Add spooky reactions. --- bot/cogs/spookyreact.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 bot/cogs/spookyreact.py (limited to 'bot/cogs') diff --git a/bot/cogs/spookyreact.py b/bot/cogs/spookyreact.py new file mode 100644 index 00000000..1afdb196 --- /dev/null +++ b/bot/cogs/spookyreact.py @@ -0,0 +1,32 @@ +from discord.ext import commands + +SPOOKY_TRIGGERS = { + 'spooky': "\U0001F47B", + 'skeleton': "\U0001F480", + 'doot': "\U0001F480", + 'pumpkin': "\U0001F383", + 'halloween': "\U0001F383", + 'jack-o-lantern': "\U0001F383" +} + + +class SpookyReact: + + """ + A cog that makes the bot react to message triggers. + """ + + def __init__(self, bot): + self.bot = bot + + async def on_message(self, ctx): + """ + A command to send the hacktoberbot github project + """ + for trigger in SPOOKY_TRIGGERS.keys(): + if trigger in ctx.content.lower(): + await ctx.add_reaction(SPOOKY_TRIGGERS[trigger]) + + +def setup(bot): + bot.add_cog(SpookyReact(bot)) -- cgit v1.2.3 From af52b46c79aab27500c273cc4262ffe57ae87fe8 Mon Sep 17 00:00:00 2001 From: hundredrab Date: Thu, 11 Oct 2018 01:54:19 +0530 Subject: Add skull-and-crossbones to spookyreacts. --- bot/cogs/spookyreact.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bot/cogs') diff --git a/bot/cogs/spookyreact.py b/bot/cogs/spookyreact.py index 1afdb196..f5534626 100644 --- a/bot/cogs/spookyreact.py +++ b/bot/cogs/spookyreact.py @@ -6,7 +6,8 @@ SPOOKY_TRIGGERS = { 'doot': "\U0001F480", 'pumpkin': "\U0001F383", 'halloween': "\U0001F383", - 'jack-o-lantern': "\U0001F383" + 'jack-o-lantern': "\U0001F383", + 'danger': "\U00002620" } -- cgit v1.2.3 From a606e483b3a42357778fc3d4979e800e8dd38739 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Thu, 11 Oct 2018 11:44:28 +0200 Subject: Adding Pale Man, fixing all flake8 issues, converting halloweenify to use aiohttp, fixing broken logging, ignoring irrelevant loggers, and turning the script into a valid module script. --- bot/__init__.py | 19 ++++++++++++------- bot/__main__.py | 39 +++++++++++++++++++++++++++++++++++++++ bot/bot.py | 39 --------------------------------------- bot/cogs/hacktoberstats.py | 9 ++++++++- bot/cogs/halloweenify.py | 4 +--- bot/cogs/movie.py | 33 ++++++++++++++++++++------------- bot/resources/halloweenify.json | 3 +++ 7 files changed, 83 insertions(+), 63 deletions(-) create mode 100644 bot/__main__.py delete mode 100644 bot/bot.py (limited to 'bot/cogs') diff --git a/bot/__init__.py b/bot/__init__.py index 8cbcd121..c2ea4ba0 100644 --- a/bot/__init__.py +++ b/bot/__init__.py @@ -1,8 +1,8 @@ -import os import logging.handlers +import os -# set up logging +# set up logging log_dir = 'log' log_file = log_dir + os.sep + 'hackbot.log' os.makedirs(log_dir, exist_ok=True) @@ -22,9 +22,14 @@ if root.handlers: for handler in root.handlers: root.removeHandler(handler) -# setup new logging configuration -logging.basicConfig(format='%(asctime)s - %(name)s %(levelname)s: %(message)s', datefmt="%D %H:%M:%S", - level=logging.DEBUG, - handlers=[console_handler, file_handler]) +# Silence irrelevant loggers +logging.getLogger("discord").setLevel(logging.ERROR) -logging.info('Logging Process Started') \ No newline at end of file +# setup new logging configuration +logging.basicConfig( + format='%(asctime)s - %(name)s %(levelname)s: %(message)s', + datefmt="%D %H:%M:%S", + level=logging.DEBUG, + handlers=[console_handler, file_handler] +) +logging.getLogger().info('Logging initialization complete') diff --git a/bot/__main__.py b/bot/__main__.py new file mode 100644 index 00000000..2c41d2d9 --- /dev/null +++ b/bot/__main__.py @@ -0,0 +1,39 @@ +import logging +from os import environ +from pathlib import Path +from traceback import format_exc + +from discord.ext import commands + +HACKTOBERBOT_TOKEN = environ.get('HACKTOBERBOT_TOKEN') +log = logging.getLogger() + +if HACKTOBERBOT_TOKEN: + token_dl = len(HACKTOBERBOT_TOKEN) // 8 + log.info(f'Bot token loaded: {HACKTOBERBOT_TOKEN[:token_dl]}...{HACKTOBERBOT_TOKEN[-token_dl:]}') +else: + log.error(f'Bot token not found: {HACKTOBERBOT_TOKEN}') + +ghost_unicode = "\N{GHOST}" +bot = commands.Bot(command_prefix=commands.when_mentioned_or(".", f"{ghost_unicode} ", ghost_unicode)) + +log.info('Start loading extensions from ./cogs/') + + +if __name__ == '__main__': + # Scan for files in the /cogs/ directory and make a list of the file names. + cogs = [file.stem for file in Path('cogs').glob('*.py')] + for extension in cogs: + try: + bot.load_extension(f'cogs.{extension}') + log.info(f'Successfully loaded extension: {extension}') + except Exception as e: + log.error(f'Failed to load extension {extension}: {repr(e)} {format_exc()}') + # print(f'Failed to load extension {extension}.', file=stderr) + # print_exc() + +log.info(f'Spooky Launch Sequence Initiated...') + +bot.run(HACKTOBERBOT_TOKEN) + +log.info(f'HackBot has been slain!') diff --git a/bot/bot.py b/bot/bot.py deleted file mode 100644 index a40ed0d4..00000000 --- a/bot/bot.py +++ /dev/null @@ -1,39 +0,0 @@ -from os import environ -from pathlib import Path -from sys import stderr -from traceback import print_exc, format_exc - -from discord.ext import commands -import logging - -HACKTOBERBOT_TOKEN = environ.get('HACKTOBERBOT_TOKEN') - -if HACKTOBERBOT_TOKEN: - token_dl = len(HACKTOBERBOT_TOKEN) // 8 - logging.info(f'Bot token loaded: {HACKTOBERBOT_TOKEN[:token_dl]}...{HACKTOBERBOT_TOKEN[-token_dl:]}') -else: - logging.error(f'Bot token not found: {HACKTOBERBOT_TOKEN}') - -ghost_unicode = "\N{GHOST}" -bot = commands.Bot(command_prefix=commands.when_mentioned_or(".", f"{ghost_unicode} ", ghost_unicode)) - -logging.info('Start loading extensions from ./cogs/') - - -if __name__ == '__main__': - # Scan for files in the /cogs/ directory and make a list of the file names. - cogs = [file.stem for file in Path('cogs').glob('*.py')] - for extension in cogs: - try: - bot.load_extension(f'cogs.{extension}') - logging.info(f'Successfully loaded extension: {extension}') - except Exception as e: - logging.error(f'Failed to load extension {extension}: {repr(e)} {format_exc()}') - # print(f'Failed to load extension {extension}.', file=stderr) - # print_exc() - -logging.info(f'Spooky Launch Sequence Initiated...') - -bot.run(HACKTOBERBOT_TOKEN) - -logging.info(f'HackBot has been slain!') \ No newline at end of file diff --git a/bot/cogs/hacktoberstats.py b/bot/cogs/hacktoberstats.py index 4e896ae9..ac81b887 100644 --- a/bot/cogs/hacktoberstats.py +++ b/bot/cogs/hacktoberstats.py @@ -95,7 +95,14 @@ class Stats: is_query = f"public+author:{username}" date_range = "2018-10-01..2018-10-31" per_page = "300" - query_url = f"{base_url}-label:{not_label}+type:{action_type}+is:{is_query}+created:{date_range}&per_page={per_page}" + query_url = ( + f"{base_url}" + f"-label:{not_label}" + f"+type:{action_type}" + f"+is:{is_query}" + f"+created:{date_range}" + f"&per_page={per_page}" + ) headers = {"user-agent": "Discord Python Hactoberbot"} async with aiohttp.ClientSession() as session: diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index 8a9db3df..ddd96bc6 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -1,15 +1,13 @@ -from pathlib import Path from json import load +from pathlib import Path from random import choice - import discord from discord.ext import commands from discord.ext.commands.cooldowns import BucketType class Halloweenify: - """ A cog to change a invokers nickname to a spooky one! """ diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py index bb6f8df8..51529bc9 100644 --- a/bot/cogs/movie.py +++ b/bot/cogs/movie.py @@ -1,8 +1,10 @@ -import requests import random from os import environ -from discord.ext import commands + +import aiohttp from discord import Embed +from discord.ext import commands + TMDB_API_KEY = environ.get('TMDB_API_KEY') TMDB_TOKEN = environ.get('TMDB_TOKEN') @@ -40,19 +42,24 @@ class Movie: } # Get total page count of horror movies - response = requests.get(url=url, params=params, headers=headers) - total_pages = response.json().get('total_pages') + async with aiohttp.ClientSession() as session: + response = await session.get(url=url, params=params, headers=headers) + total_pages = await response.json() + total_pages = total_pages.get('total_pages') - # Get movie details from one random result on a random page - params['page'] = random.randint(1, total_pages) - response = requests.get(url=url, params=params, headers=headers) - selection_id = random.choice(response.json().get('results')).get('id') + # Get movie details from one random result on a random page + params['page'] = random.randint(1, total_pages) + response = await session.get(url=url, params=params, headers=headers) + response = await response.json() + selection_id = random.choice(response.get('results')).get('id') - # Get full details and credits - selection = requests.get(url='https://api.themoviedb.org/3/movie/' + str(selection_id), - params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'}) + # Get full details and credits + selection = await session.get( + url='https://api.themoviedb.org/3/movie/' + str(selection_id), + params={'api_key': TMDB_API_KEY, 'append_to_response': 'credits'} + ) - return selection.json() + return await selection.json() @staticmethod async def format_metadata(movie): @@ -72,7 +79,7 @@ class Movie: rating_count = movie.get('vote_average') / 2 rating = '' - for i in range(int(rating_count)): + for _ in range(int(rating_count)): rating += ':skull:' if (rating_count % 1) >= .5: diff --git a/bot/resources/halloweenify.json b/bot/resources/halloweenify.json index 458f9342..88c46bfc 100644 --- a/bot/resources/halloweenify.json +++ b/bot/resources/halloweenify.json @@ -74,6 +74,9 @@ }, { "Chatterer": "https://c-5uwzmx78pmca09x24quoqfx2ezivsmzx2ekwu.g00.ranker.com/g00/3_c-5eee.zivsmz.kwu_/c-5UWZMXPMCA09x24pbbx78ax3ax2fx2fquoqf.zivsmz.kwux2fvwlm_quox2f14x2f586061x2fwzqoqvitx2fkpibbmzmz-nqtu-kpizikbmza-x78pwbw-9x3fex3d438x26yx3d48x26nux3drx78ox26nqbx3dkzwx78x26kzwx78x3dnikmax22x26q98k.uizsx3dquiom_$/$/$/$/$/$" + }, + { + "Pale Man": "https://i2.wp.com/macguff.in/wp-content/uploads/2016/10/Pans-Labyrinth-Movie-Header-Image.jpg?fit=630%2C400&ssl=1" } ] } \ No newline at end of file -- cgit v1.2.3 From 5e2abfd06ebe5d3011fbbcc818d85e215f358969 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Thu, 11 Oct 2018 12:12:32 +0200 Subject: Made the movie formatter less naïve. It no longer crashes if TMDB's return data is incomplete. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/cogs/movie.py | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py index 51529bc9..82b9c682 100644 --- a/bot/cogs/movie.py +++ b/bot/cogs/movie.py @@ -67,36 +67,63 @@ class Movie: Formats raw TMDb data to be embedded in discord chat """ - tmdb_url = 'https://www.themoviedb.org/movie/' + str(movie.get('id')) - poster = 'https://image.tmdb.org/t/p/original' + movie.get('poster_path') + # Build the relevant URLs. + movie_id = movie.get("id") + poster_path = movie.get("poster_path") + tmdb_url = f'https://www.themoviedb.org/movie/{movie_id}' if movie_id else None + poster = f'https://image.tmdb.org/t/p/original{poster_path}' if poster_path else None + # Get cast names cast = [] - for actor in movie.get('credits').get('cast')[:3]: + for actor in movie.get('credits', {}).get('cast', [])[:3]: cast.append(actor.get('name')) - director = movie.get('credits').get('crew')[0].get('name') + # Get director name + director = movie.get('credits', {}).get('crew', []) + if director: + director = director[0].get('name') - rating_count = movie.get('vote_average') / 2 + # Determine the spookiness rating rating = '' + rating_count = movie.get('vote_average', 0) + + if rating_count: + rating_count /= 2 for _ in range(int(rating_count)): rating += ':skull:' - if (rating_count % 1) >= .5: rating += ':bat:' + # Try to get year of release and runtime + year = movie.get('release_date', [])[:4] + runtime = movie.get('runtime') + runtime = f"{runtime} minutes" if runtime else None + + # Not all these attributes will always be present + movie_attributes = { + "Directed by": director, + "Starring": ', '.join(cast), + "Running time": runtime, + "Release year": year, + "Spookiness rating": rating, + } + embed = Embed( colour=0x01d277, title='**' + movie.get('title') + '**', url=tmdb_url, description=movie.get('overview') ) - embed.set_image(url=poster) - embed.add_field(name='Starring', value=', '.join(cast)) - embed.add_field(name='Directed by', value=director) - embed.add_field(name='Year', value=movie.get('release_date')[:4]) - embed.add_field(name='Runtime', value=str(movie.get('runtime')) + ' min') - embed.add_field(name='Spooky Rating', value=rating) + + if poster: + embed.set_image(url=poster) + + # Add the attributes that we actually have data for, but not the others. + for name, value in movie_attributes.items(): + if value: + embed.add_field(name=name, value=value) + embed.set_footer(text='powered by themoviedb.org') return embed -- cgit v1.2.3 From 0c76e002e1da343def4787c6b1bfcc1970aea853 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Thu, 11 Oct 2018 19:57:28 +0100 Subject: Linting. Thanks fellas. --- bot/cogs/spookyreact.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/spookyreact.py b/bot/cogs/spookyreact.py index f5534626..2652a60e 100644 --- a/bot/cogs/spookyreact.py +++ b/bot/cogs/spookyreact.py @@ -1,5 +1,3 @@ -from discord.ext import commands - SPOOKY_TRIGGERS = { 'spooky': "\U0001F47B", 'skeleton': "\U0001F480", -- cgit v1.2.3 From 79dc1d87824b6ab1afc2832f4e40c981ac8f0e20 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 12 Oct 2018 00:27:22 +0200 Subject: improving the .help docstrings, and making the bot load cogs from the correct path. (#43) * Improving some docstrings and making it look in ./bot/cogs for cogs. * Removing pointless stuff from a docstring --- bot/__main__.py | 6 +++--- bot/cogs/halloweenify.py | 3 +++ bot/cogs/movie.py | 5 ++++- bot/cogs/template.py | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) (limited to 'bot/cogs') diff --git a/bot/__main__.py b/bot/__main__.py index 2c41d2d9..ccd69b0b 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -17,15 +17,15 @@ else: ghost_unicode = "\N{GHOST}" bot = commands.Bot(command_prefix=commands.when_mentioned_or(".", f"{ghost_unicode} ", ghost_unicode)) -log.info('Start loading extensions from ./cogs/') +log.info('Start loading extensions from ./bot/cogs/') if __name__ == '__main__': # Scan for files in the /cogs/ directory and make a list of the file names. - cogs = [file.stem for file in Path('cogs').glob('*.py')] + cogs = [file.stem for file in Path('bot', 'cogs').glob('*.py')] for extension in cogs: try: - bot.load_extension(f'cogs.{extension}') + bot.load_extension(f'bot.cogs.{extension}') log.info(f'Successfully loaded extension: {extension}') except Exception as e: log.error(f'Failed to load extension {extension}: {repr(e)} {format_exc()}') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index ddd96bc6..3bd04b80 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -18,6 +18,9 @@ class Halloweenify: @commands.cooldown(1, 300, BucketType.user) @commands.command() async def halloweenify(self, ctx): + """ + Change your nickname into a much spookier one! + """ with open(Path('../bot/resources', 'halloweenify.json'), 'r') as f: data = load(f) diff --git a/bot/cogs/movie.py b/bot/cogs/movie.py index 82b9c682..925f813f 100644 --- a/bot/cogs/movie.py +++ b/bot/cogs/movie.py @@ -18,8 +18,11 @@ class Movie: def __init__(self, bot): self.bot = bot - @commands.command(name='movie', alias=['tmdb'], brief='Pick a scary movie') + @commands.command(name='movie', alias=['tmdb']) async def random_movie(self, ctx): + """ + Randomly select a scary movie and display information about it. + """ selection = await self.select_movie() movie_details = await self.format_metadata(selection) diff --git a/bot/cogs/template.py b/bot/cogs/template.py index b3f4da21..aa01432c 100644 --- a/bot/cogs/template.py +++ b/bot/cogs/template.py @@ -17,7 +17,7 @@ class Template: """ await ctx.send('https://github.com/discord-python/hacktoberbot') - @commands.group(name='git', invoke_without_command=True) + @commands.group(name='git', invoke_without_command=True, brief="A link to resources for learning Git") async def github(self, ctx): """ A command group with the name git. You can now create sub-commands such as git commit. -- cgit v1.2.3 From 3230a505b1e5aa84e79a0afc09e10da6d433120d Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Fri, 12 Oct 2018 00:36:53 +0200 Subject: Fixing halloweenify json path --- bot/cogs/halloweenify.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot/cogs') diff --git a/bot/cogs/halloweenify.py b/bot/cogs/halloweenify.py index ddd96bc6..a5fe45ef 100644 --- a/bot/cogs/halloweenify.py +++ b/bot/cogs/halloweenify.py @@ -18,7 +18,10 @@ class Halloweenify: @commands.cooldown(1, 300, BucketType.user) @commands.command() async def halloweenify(self, ctx): - with open(Path('../bot/resources', 'halloweenify.json'), 'r') as f: + """ + Change your nickname into a much spookier one! + """ + with open(Path('./bot/resources', 'halloweenify.json'), 'r') as f: data = load(f) # Choose a random character from our list we loaded above and set apart the nickname and image url. -- cgit v1.2.3