games.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. from discord.ext import commands
  2. import discord
  3. import random
  4. from typing import Optional
  5. from query.channel import get_games
  6. from query.user import is_ignored, get_level, get_xp, level_up
  7. from local_settings import COMMAND_PREFIX
  8. def setup(bot: commands.Bot):
  9. bot.add_cog(Games(bot))
  10. class Games(commands.Cog):
  11. """Gaming commands."""
  12. def __init__(self, bot: commands.Bot):
  13. self.bot = bot
  14. @commands.command(
  15. description="Check the level for a player.",
  16. brief="Get player level",
  17. help="View game level of player."
  18. )
  19. async def level(self, ctx: commands.Context, user: Optional[discord.User]):
  20. # Ignore user if on the ignore list.
  21. if await is_ignored(self.bot.pg, ctx.author.id):
  22. return
  23. # Warn if games are off.
  24. if not await get_games(self.bot.pg, ctx.channel.id):
  25. ctx.author.send(f"Games are disabled in {ctx.channel}, ask an admin to enable them.")
  26. return
  27. if not user:
  28. user = ctx.author
  29. level = await get_level(self.bot.pg, user)
  30. if level == 0:
  31. if ctx.author == user:
  32. ctx.send(f"You are not playing, join the game with {COMMAND_PREFIX}`levelup`")
  33. else:
  34. ctx.send(f"{user} is not playing.")
  35. else:
  36. xp_spent, total_xp = get_xp(user)
  37. if ctx.author == user:
  38. ctx.send(f"You rank at level {level}. ({xp_spent}/{total_xp})")
  39. else:
  40. ctx.send(f"{user} ranks at level {level}. ({xp_spent}/{total_xp})")
  41. @commands.command(
  42. description="Check the experience points for a player.",
  43. brief="Get player xp",
  44. help="View amount of XP a game player has."
  45. )
  46. async def xp(self, ctx: commands.Context, user: Optional[discord.User]):
  47. # Ignore user if on the ignore list.
  48. if await is_ignored(self.bot.pg, ctx.author.id):
  49. return
  50. # Warn if games are off.
  51. if not await get_games(self.bot.pg, ctx.channel.id):
  52. ctx.author.send(f"Games are disabled in {ctx.channel}, ask an admin to enable them.")
  53. return
  54. if not user:
  55. xp_spent, total_xp = await get_xp(self.bot.pg, ctx.author.id)
  56. ctx.send(f"You have spent {xp_spent} experience points of your {total_xp} total.")
  57. else:
  58. xp_spent, total_xp = await get_xp(self.bot.pg, user)
  59. ctx.send(f"{user} has spent {xp_spent} of {total_xp} experience points.")
  60. @commands.command(
  61. description="Attempt to gain a level.",
  62. brief="Level up",
  63. help="Try to rank up a level in the game by spending XP."
  64. )
  65. async def levelup(self, ctx: commands.Context, user: Optional[discord.User]):
  66. # Ignore user if on the ignore list.
  67. if await is_ignored(self.bot.pg, ctx.author.id):
  68. return
  69. # Warn if games are off.
  70. if not await get_games(self.bot.pg, ctx.channel.id):
  71. ctx.author.send(f"Games are disabled in {ctx.channel}, ask an admin to enable them.")
  72. return
  73. xp_spent, total_xp = await get_xp(self.bot.pg, ctx.author.id)
  74. xp_available = total_xp - xp_spent
  75. level = await get_level(self.bot.pg, user)
  76. threshold = (level + 1) * 50 + xp_spent
  77. if xp_available < threshold:
  78. ctx.send(f"Not yet, you require {threshold - xp_available} more XP to level up.")
  79. else:
  80. level_up(self.bot.pg, ctx.author.id, threshold)
  81. ctx.send(f"You have climbed the ranks for {threshold} XP, leaving you {xp_available - threshold} remaining.")
  82. @commands.command(
  83. description="Simulate dice rolls.",
  84. brief="Roll dice",
  85. help="Roll two dice."
  86. )
  87. async def dice(self, ctx: commands.Context, amount: Optional[int], sides: Optional[int]):
  88. # Ignore user if on the ignore list.
  89. if await is_ignored(self.bot.pg, ctx.author.id):
  90. return
  91. # Warn if games are off.
  92. if not await get_games(self.bot.pg, ctx.channel.id):
  93. ctx.author.send(f"Games are disabled in {ctx.channel}, ask an admin to enable them.")
  94. return
  95. if not amount:
  96. amount = 2
  97. if not sides:
  98. sides = 6
  99. if amount < 1:
  100. await ctx.send("You want me to roll less than one die? How!?")
  101. elif amount > 25:
  102. await ctx.send("I can not hold so many dice at one time.")
  103. elif sides < 2:
  104. await ctx.send("A die has physical minimum of 2 sides. Don't ask for impossible objects.")
  105. elif sides > 256:
  106. await ctx.send("My tiny hands can not handle such large dice. Even if both are virtual.")
  107. else:
  108. embed = discord.Embed(title = "Dice roll", description=f"Rolling {amount} dice, with {sides} sides.")
  109. while amount > 0:
  110. embed.insert_field_at(0, name=f"Die {amount}", value=random.randint(1, sides), inline=True)
  111. amount -= 1
  112. await ctx.send(embed=embed)
  113. @commands.command(
  114. description="Ask the magic 8-ball.",
  115. brief="Pose question",
  116. help="Simulate the iconic 8-ball gimmic.",
  117. name="8ball"
  118. )
  119. async def eightball(self, ctx: commands.Context, *, question: str = None):
  120. # Ignore user if on the ignore list.
  121. if await is_ignored(self.bot.pg, ctx.author.id):
  122. return
  123. if not await get_games(self.bot.pg, ctx.channel.id):
  124. ctx.author.send(f"Games are disabled in {ctx.channel}, ask an admin to enable them.")
  125. return
  126. if not question:
  127. messages = [
  128. "Don't forget to ask a question...",
  129. "Hey, that's not a question!",
  130. "What would you like to know?",
  131. "You want me to predict nothing?",
  132. "Are you intentionally not asking a question?",
  133. "Ask a question you tease!",
  134. "You will die alone.",
  135. ]
  136. elif question.strip().count(" ") == 0:
  137. messages = [
  138. "What?",
  139. "That is not a question",
  140. "Can you use more than one word?",
  141. "What is the question?",
  142. "Sorry?"
  143. ]
  144. elif question.strip()[-1] != "?":
  145. messages = [
  146. "Did you forget to end with a question mark?",
  147. "Is that a statement or question?",
  148. "Don't questions usually end with a question mark?",
  149. "Don't forget to use punctuation."
  150. ]
  151. else:
  152. messages = [
  153. "Yes.",
  154. "No.",
  155. "Affirmative.",
  156. "No way!",
  157. "Negative.",
  158. "Positive.",
  159. "Correct.",
  160. "Incorrect.",
  161. "Likely",
  162. "Unlikely",
  163. "Maybe.",
  164. "Definately!",
  165. "Perhaps?",
  166. "Most indubitably.",
  167. "Does the pope shit in the woods?",
  168. "When hell freezes over.",
  169. "Only between 9 and 5.",
  170. "Only just before you die.",
  171. "ERROR: Probability failure.",
  172. "Ask again later.",
  173. "I don't know.",
  174. "Unpredictable.",
  175. "Unknown",
  176. ]
  177. await ctx.send(random.choice(messages))