[General] Refactored [p]rps

This commit is contained in:
Twentysix 2016-12-21 18:11:04 +01:00
parent 5c00d39f69
commit 08541e419f

View File

@ -2,7 +2,8 @@ import discord
from discord.ext import commands from discord.ext import commands
from .utils.chat_formatting import * from .utils.chat_formatting import *
from random import randint from random import randint
from random import choice as randchoice from random import choice
from enum import Enum
import datetime import datetime
import time import time
import aiohttp import aiohttp
@ -10,6 +11,26 @@ import asyncio
settings = {"POLL_DURATION" : 60} settings = {"POLL_DURATION" : 60}
class RPS(Enum):
rock = "\N{MOYAI}"
paper = "\N{PAGE FACING UP}"
scissors = "\N{BLACK SCISSORS}"
class RPSParser:
def __init__(self, argument):
argument = argument.lower()
if argument == "rock":
self.choice = RPS.rock
elif argument == "paper":
self.choice = RPS.paper
elif argument == "scissors":
self.choice = RPS.scissors
else:
raise
class General: class General:
"""General commands.""" """General commands."""
@ -33,11 +54,11 @@ class General:
To denote multiple choices, you should use double quotes. To denote multiple choices, you should use double quotes.
""" """
choices = [escape_mass_mentions(choice) for choice in choices] choices = [escape_mass_mentions(c) for c in choices]
if len(choices) < 2: if len(choices) < 2:
await self.bot.say('Not enough choices to pick from.') await self.bot.say('Not enough choices to pick from.')
else: else:
await self.bot.say(randchoice(choices)) await self.bot.say(choice(choices))
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def roll(self, ctx, number : int = 100): async def roll(self, ctx, number : int = 100):
@ -73,39 +94,37 @@ class General:
name = name.translate(table) name = name.translate(table)
await self.bot.say(msg + "(╯°□°)╯︵ " + name[::-1]) await self.bot.say(msg + "(╯°□°)╯︵ " + name[::-1])
else: else:
await self.bot.say("*flips a coin and... " + randchoice(["HEADS!*", "TAILS!*"])) await self.bot.say("*flips a coin and... " + choice(["HEADS!*", "TAILS!*"]))
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def rps(self, ctx, choice : str): async def rps(self, ctx, your_choice : RPSParser):
"""Play rock paper scissors""" """Play rock paper scissors"""
author = ctx.message.author author = ctx.message.author
rpsbot = {"rock" : ":moyai:", player_choice = your_choice.choice
"paper": ":page_facing_up:", red_choice = choice((RPS.rock, RPS.paper, RPS.scissors))
"scissors":":scissors:"} cond = {
choice = choice.lower() (RPS.rock, RPS.paper) : False,
if choice in rpsbot.keys(): (RPS.rock, RPS.scissors) : True,
botchoice = randchoice(list(rpsbot.keys())) (RPS.paper, RPS.rock) : True,
msgs = { (RPS.paper, RPS.scissors) : False,
"win": " You win {}!".format(author.mention), (RPS.scissors, RPS.rock) : False,
"square": " We're square {}!".format(author.mention), (RPS.scissors, RPS.paper) : True
"lose": " You lose {}!".format(author.mention) }
}
if choice == botchoice: if red_choice == player_choice:
await self.bot.say(rpsbot[botchoice] + msgs["square"]) outcome = None # Tie
elif choice == "rock" and botchoice == "paper":
await self.bot.say(rpsbot[botchoice] + msgs["lose"])
elif choice == "rock" and botchoice == "scissors":
await self.bot.say(rpsbot[botchoice] + msgs["win"])
elif choice == "paper" and botchoice == "rock":
await self.bot.say(rpsbot[botchoice] + msgs["win"])
elif choice == "paper" and botchoice == "scissors":
await self.bot.say(rpsbot[botchoice] + msgs["lose"])
elif choice == "scissors" and botchoice == "rock":
await self.bot.say(rpsbot[botchoice] + msgs["lose"])
elif choice == "scissors" and botchoice == "paper":
await self.bot.say(rpsbot[botchoice] + msgs["win"])
else: else:
await self.bot.say("Choose rock, paper or scissors.") outcome = cond[(player_choice, red_choice)]
if outcome is True:
await self.bot.say("{} You win {}!"
"".format(red_choice.value, author.mention))
elif outcome is False:
await self.bot.say("{} You lose {}!"
"".format(red_choice.value, author.mention))
else:
await self.bot.say("{} We're square {}!"
"".format(red_choice.value, author.mention))
@commands.command(name="8", aliases=["8ball"]) @commands.command(name="8", aliases=["8ball"])
async def _8ball(self, *, question : str): async def _8ball(self, *, question : str):
@ -114,7 +133,7 @@ class General:
Question must end with a question mark. Question must end with a question mark.
""" """
if question.endswith("?") and question != "?": if question.endswith("?") and question != "?":
await self.bot.say("`" + randchoice(self.ball) + "`") await self.bot.say("`" + choice(self.ball) + "`")
else: else:
await self.bot.say("That doesn't look like a question.") await self.bot.say("That doesn't look like a question.")
@ -230,7 +249,7 @@ class General:
"".format(server.created_at.strftime("%d %b %Y %H:%M"), "".format(server.created_at.strftime("%d %b %Y %H:%M"),
passed)) passed))
colour = ''.join([randchoice('0123456789ABCDEF') for x in range(6)]) colour = ''.join([choice('0123456789ABCDEF') for x in range(6)])
colour = int(colour, 16) colour = int(colour, 16)
data = discord.Embed( data = discord.Embed(