Skip to content

Instantly share code, notes, and snippets.

@crazygmr101
Created June 5, 2020 21:59
Show Gist options
  • Select an option

  • Save crazygmr101/e2d03f460ef601dfd5f859c2c14e50a2 to your computer and use it in GitHub Desktop.

Select an option

Save crazygmr101/e2d03f460ef601dfd5f859c2c14e50a2 to your computer and use it in GitHub Desktop.
async def _input(self, ctx, typ: type, cancel_str: str = "cancel", ch: Callable = None, err=None, check_author=True,
return_author=False, del_error=60, del_response=False):
def check(m):
return ((m.author == ctx.author and m.channel == ctx.channel) or not check_author) and not m.author.bot
while True:
try:
inp: discord.Message = await self.bot.wait_for('message', check=check, timeout=60.0)
if del_response:
await inp.delete()
if inp.content.lower() == cancel_str.lower():
return (None, None) if return_author else None
res = typ(inp.content.lower())
if ch:
if not ch(res): raise ValueError
return (res, inp.author) if return_author else res
except ValueError:
await ctx.send(err or "That's not a valid response, try again" +
("" if not cancel_str else f" or type `{cancel_str}` to quit"), delete_after=del_error)
continue
except asyncio.TimeoutError:
await ctx.send("You took too long to respond ): Try to start over", delete_after=del_error)
return (None, None) if return_author else None
@commands.command()
async def rps(self, ctx: commands.Context):
"""
Play rock-paper-scissors with the bot
"""
def point(a: str, b: str):
a, b = a[0], b[0]
return {
"rr": 0,
"pp": 0,
"ss": 0,
"rp": 1,
"pr": -1,
"sp": -1,
"ps": 1,
"sr": 1,
"rs": -1
}[a + b]
conv = {
"r": "Rock",
"p": "Paper",
"s": "Scissors"
}
win = ["You got a point!", "It's a tie!", "I got a point!"]
final = ["You win!", "It's a tie!", "I win."]
if not await _check_channel(ctx): return
await ctx.send("How many rounds? 1-10")
rounds = await self._input(ctx, int, "cancel", lambda x: 0 < x <= 10)
user = 0
comp = 0
for i in range(0, rounds):
await ctx.send(f"Round {i + 1} - You: {user} | Me: {comp}\n"
f"Input R P or S")
choice = (await self._input(ctx, str, "cancel", lambda x: x[0].lower() in ["r", "p", "s"]))[0]
if not choice: break
compch = random.choice(["r", "p", "s"])
dev = point(choice, compch)
if dev == 1: comp += 1
if dev == -1: user += 1
await ctx.send(f"{win[dev + 1]} I got {conv[compch]}, and you got {conv[choice]}")
await ctx.send(f"Game over\n{final[1 if comp == user else 2 if comp > user else 0]} - You: {user} | Me: {comp}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment