1
0

games.py 7.1 KB

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