games.py 7.1 KB

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