diff options
author | 2021-06-07 16:55:17 +0100 | |
---|---|---|
committer | 2021-06-07 16:55:17 +0100 | |
commit | 4828e93eedadbcaec23807bad3f884e9087e5c72 (patch) | |
tree | 5233eab4b5070b5c22117830f856a653770a783d /bot/exts | |
parent | Removed import (diff) | |
parent | Merge pull request #595 from Shivansh-007/fix/ttt (diff) |
Merge branch 'main' into main
Diffstat (limited to 'bot/exts')
-rw-r--r-- | bot/exts/evergreen/rps.py | 57 | ||||
-rw-r--r-- | bot/exts/evergreen/tic_tac_toe.py | 14 | ||||
-rw-r--r-- | bot/exts/pride/pride_anthem.py | 2 | ||||
-rw-r--r-- | bot/exts/pride/pride_facts.py | 2 | ||||
-rw-r--r-- | bot/exts/pride/pride_leader.py | 117 |
5 files changed, 185 insertions, 7 deletions
diff --git a/bot/exts/evergreen/rps.py b/bot/exts/evergreen/rps.py new file mode 100644 index 00000000..c6bbff46 --- /dev/null +++ b/bot/exts/evergreen/rps.py @@ -0,0 +1,57 @@ +from random import choice + +from discord.ext import commands + +from bot.bot import Bot + +CHOICES = ["rock", "paper", "scissors"] +SHORT_CHOICES = ["r", "p", "s"] + +# Using a dictionary instead of conditions to check for the winner. +WINNER_DICT = { + "r": { + "r": 0, + "p": -1, + "s": 1, + }, + "p": { + "r": 1, + "p": 0, + "s": -1, + }, + "s": { + "r": -1, + "p": 1, + "s": 0, + } +} + + +class RPS(commands.Cog): + """Rock Paper Scissors. The Classic Game!""" + + @commands.command(case_insensitive=True) + async def rps(self, ctx: commands.Context, move: str) -> None: + """Play the classic game of Rock Paper Scissors with your own sir-lancebot!""" + move = move.lower() + player_mention = ctx.author.mention + + if move not in CHOICES and move not in SHORT_CHOICES: + raise commands.BadArgument(f"Invalid move. Please make move from options: {', '.join(CHOICES).upper()}.") + + bot_move = choice(CHOICES) + # value of player_result will be from (-1, 0, 1) as (lost, tied, won). + player_result = WINNER_DICT[move[0]][bot_move[0]] + + if player_result == 0: + message_string = f"{player_mention} You and Sir Lancebot played {bot_move}, it's a tie." + await ctx.send(message_string) + elif player_result == 1: + await ctx.send(f"Sir Lancebot played {bot_move}! {player_mention} won!") + else: + await ctx.send(f"Sir Lancebot played {bot_move}! {player_mention} lost!") + + +def setup(bot: Bot) -> None: + """Load the RPS Cog.""" + bot.add_cog(RPS(bot)) diff --git a/bot/exts/evergreen/tic_tac_toe.py b/bot/exts/evergreen/tic_tac_toe.py index bd5e0102..48e8e142 100644 --- a/bot/exts/evergreen/tic_tac_toe.py +++ b/bot/exts/evergreen/tic_tac_toe.py @@ -79,7 +79,7 @@ class AI: """Get move from AI. AI use Minimax strategy.""" possible_moves = [i for i, emoji in board.items() if emoji in list(Emojis.number_emojis.values())] - for symbol in (Emojis.o, Emojis.x): + for symbol in (Emojis.o_square, Emojis.x_square): for move in possible_moves: board_copy = board.copy() board_copy[move] = symbol @@ -265,12 +265,12 @@ class TicTacToe(Cog): return if opponent is None: game = Game( - [Player(ctx.author, ctx, Emojis.x), AI(Emojis.o)], + [Player(ctx.author, ctx, Emojis.x_square), AI(Emojis.o_square)], ctx ) else: game = Game( - [Player(ctx.author, ctx, Emojis.x), Player(opponent, ctx, Emojis.o)], + [Player(ctx.author, ctx, Emojis.x_square), Player(opponent, ctx, Emojis.o_square)], ctx ) self.games.append(game) @@ -317,8 +317,12 @@ class TicTacToe(Cog): await ctx.send("Game don't exist.") return game = self.games[game_id - 1] - await ctx.send(f"{game.winner} :trophy: vs {game.loser}") - await ctx.send(game.format_board()) + + embed = discord.Embed( + title=f"Match #{game_id} Game Board", + description=f"{game.winner} :trophy: vs {game.loser}\n\n{game.format_board()}" + ) + await ctx.send(embed=embed) def setup(bot: Bot) -> None: diff --git a/bot/exts/pride/pride_anthem.py b/bot/exts/pride/pride_anthem.py index 4650595a..05286b3d 100644 --- a/bot/exts/pride/pride_anthem.py +++ b/bot/exts/pride/pride_anthem.py @@ -24,7 +24,7 @@ class PrideAnthem(commands.Cog): If none can be found, it will log this as well as provide that information to the user. """ if not genre: - return random.choice(self.anthems) + return random.choice(VIDEOS) else: songs = [song for song in VIDEOS if genre.casefold() in song["genre"]] try: diff --git a/bot/exts/pride/pride_facts.py b/bot/exts/pride/pride_facts.py index 631e2e8b..63e33dda 100644 --- a/bot/exts/pride/pride_facts.py +++ b/bot/exts/pride/pride_facts.py @@ -57,7 +57,7 @@ class PrideFacts(commands.Cog): date = _date if date.year < now.year or (date.year == now.year and date.day <= now.day): try: - await target.send(embed=self.make_embed(self.facts[str(date.year)][date.day - 1])) + await target.send(embed=self.make_embed(FACTS[str(date.year)][date.day - 1])) except KeyError: await target.send(f"The year {date.year} is not yet supported") return diff --git a/bot/exts/pride/pride_leader.py b/bot/exts/pride/pride_leader.py new file mode 100644 index 00000000..c3426ad1 --- /dev/null +++ b/bot/exts/pride/pride_leader.py @@ -0,0 +1,117 @@ +import json +import logging +import random +from pathlib import Path +from typing import Optional + +import discord +from discord.ext import commands +from fuzzywuzzy import fuzz + +from bot import bot +from bot import constants + +log = logging.getLogger(__name__) + +PRIDE_RESOURCE = json.loads(Path("bot/resources/pride/prideleader.json").read_text("utf8")) +MINIMUM_FUZZ_RATIO = 40 + + +class PrideLeader(commands.Cog): + """Gives information about Pride Leaders.""" + + def __init__(self, bot: bot.Bot): + self.bot = bot + + def invalid_embed_generate(self, pride_leader: str) -> discord.Embed: + """ + Generates Invalid Embed. + + The invalid embed contains a list of closely matched names of the invalid pride + leader the user gave. If no closely matched names are found it would list all + the available pride leader names. + + Wikipedia is a useful place to learn about pride leaders and we don't have all + the pride leaders, so the bot would add a field containing the wikipedia + command to execute. + """ + embed = discord.Embed( + color=constants.Colours.soft_red + ) + valid_names = [] + pride_leader = pride_leader.title() + for name in PRIDE_RESOURCE: + if fuzz.ratio(pride_leader, name) >= MINIMUM_FUZZ_RATIO: + valid_names.append(name) + + if not valid_names: + valid_names = ", ".join(PRIDE_RESOURCE) + error_msg = "Sorry your input didn't match any stored names, here is a list of available names:" + else: + valid_names = "\n".join(valid_names) + error_msg = "Did you mean?" + + embed.description = f"{error_msg}\n```{valid_names}```" + embed.set_footer(text="To add more pride leaders, feel free to open a pull request!") + + return embed + + def embed_builder(self, pride_leader: dict) -> discord.Embed: + """Generate an Embed with information about a pride leader.""" + name = [name for name, info in PRIDE_RESOURCE.items() if info == pride_leader][0] + + embed = discord.Embed( + title=name, + description=pride_leader["About"], + color=constants.Colours.blue + ) + embed.add_field( + name="Known for", + value=pride_leader["Known for"], + inline=False + ) + embed.add_field( + name="D.O.B and Birth place", + value=pride_leader["Born"], + inline=False + ) + embed.add_field( + name="Awards and honors", + value=pride_leader["Awards"], + inline=False + ) + embed.add_field( + name="For More Information", + value=f"Do `{constants.Client.prefix}wiki {name}`" + f" in <#{constants.Channels.community_bot_commands}>", + inline=False + ) + embed.set_thumbnail(url=pride_leader["url"]) + return embed + + @commands.command(aliases=("pl", "prideleader")) + async def pride_leader(self, ctx: commands.Context, *, pride_leader_name: Optional[str]) -> None: + """ + Information about a Pride Leader. + + Returns information about the specified pride leader + and if there is no pride leader given, return a random pride leader. + """ + if not pride_leader_name: + leader = random.choice(list(PRIDE_RESOURCE.values())) + else: + leader = PRIDE_RESOURCE.get(pride_leader_name.title()) + if not leader: + log.trace(f"Got a Invalid pride leader: {pride_leader_name}") + + embed = self.invalid_embed_generate(pride_leader_name) + await ctx.send(embed=embed) + return + + embed = self.embed_builder(leader) + await ctx.send(embed=embed) + + +def setup(bot: bot.Bot) -> None: + """Load the Pride Leader Cog.""" + bot.add_cog(PrideLeader(bot)) |