1
0

games.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from discord.ext import commands
  2. import discord
  3. from discord import app_commands
  4. import random
  5. from typing import Optional
  6. 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
  7. from common.settings import check_ignore_interaction
  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. @app_commands.command(
  15. name="level",
  16. description="Check the level for a player.",
  17. )
  18. async def level(self, interaction: discord.Interaction, user: Optional[discord.User]) -> None:
  19. # Keep interaction alive longer then 3 seconds to give the bot time to respond.
  20. await interaction.response.defer()
  21. # Halt on ignore list or games channel settings.
  22. if await check_ignore_interaction(self.bot.pg, interaction, games=True):
  23. return
  24. if not user:
  25. user = interaction.user
  26. level = await get_level(self.bot.pg, user.id)
  27. if level == 0:
  28. if interaction.user == user:
  29. await interaction.followup.send(f"You are not playing, join the game with `/levelup`")
  30. else:
  31. await interaction.followup.send(f"`{user}` is not playing.")
  32. else:
  33. xp_spent, total_xp = await get_xp(self.bot.pg, user.id)
  34. ability_points_spent = await get_ability_points_spent(self.bot.pg, user.id)
  35. coin = await get_coin(self.bot.pg, user.id)
  36. karma = await get_karma(self.bot.pg, user.id)
  37. if interaction.user == user:
  38. await interaction.followup.send(f"You rank at level **{level}**. (exp **{xp_spent}**/{total_xp} | abp **{ability_points_spent}**/{level * 3} | coin **{coin}** | karma **{karma}**)")
  39. else:
  40. await interaction.followup.send(f"`{user}` ranks at level **{level}**. (**{xp_spent}**/{total_xp} | abp **{ability_points_spent}**/{level * 3}) | coin **{coin}** | karma **{karma}**")
  41. @app_commands.command(
  42. name="xp",
  43. description="Check the experience points for a player.",
  44. )
  45. async def xp(self, interaction: discord.Interaction, user: Optional[discord.User]) -> None:
  46. # Keep interaction alive longer then 3 seconds to give the bot time to respond.
  47. await interaction.response.defer()
  48. # Halt on ignore list or games channel settings.
  49. if await check_ignore_interaction(self.bot.pg, interaction, games=True):
  50. return
  51. if not user:
  52. xp_spent, total_xp = await get_xp(self.bot.pg, interaction.user.id)
  53. level = await get_level(self.bot.pg, interaction.user.id)
  54. threshold = (level + 1) * 50 + xp_spent
  55. if threshold < total_xp - xp_spent:
  56. await interaction.followup.send(f"You have spent {xp_spent} experience points of your {total_xp} total and can gain 3 ability points for {threshold} xp.")
  57. else:
  58. await interaction.followup.send(f"You have spent {xp_spent} experience points of your {total_xp} total and require {threshold - (total_xp - xp_spent)} xp to `/levelup`.")
  59. else:
  60. xp_spent, total_xp = await get_xp(self.bot.pg, user.id)
  61. level = await get_level(self.bot.pg, user.id)
  62. threshold = (level + 1) * 50 + xp_spent
  63. if threshold < total_xp - xp_spent:
  64. await interaction.followup.send(f"`{user}` has spent {xp_spent} of {total_xp} experience points and can level up for {threshold} xp.")
  65. else:
  66. await interaction.followup.send(f"`{user}` has spent {xp_spent} of {total_xp} experience points and requires {threshold - (total_xp - xp_spent)} xp to level up.")
  67. @app_commands.command(
  68. name="levelup",
  69. description="Attempt to gain a level.",
  70. )
  71. async def levelup(self, interaction: discord.Interaction) -> None:
  72. # Halt on ignore list or games channel settings.
  73. if await check_ignore_interaction(self.bot.pg, interaction, games=True):
  74. return
  75. xp_spent, total_xp = await get_xp(self.bot.pg, interaction.user.id)
  76. xp_available = total_xp - xp_spent
  77. level = await get_level(self.bot.pg, interaction.user.id)
  78. threshold = (level + 1) * 50 + xp_spent
  79. if xp_available < threshold:
  80. await interaction.followup.send(f"Not yet, you require {threshold - xp_available} more XP to level up.")
  81. else:
  82. await level_up(self.bot.pg, interaction.user.id, threshold)
  83. await interaction.send_message(f"You have gained three ability points climbed the ranks for {threshold} XP, leaving you {xp_available - threshold} remaining.")
  84. await increment_all_coin(self.bot.pg)
  85. @app_commands.command(
  86. name="rob",
  87. description="Rob another player",
  88. )
  89. async def rob(self, interaction: discord.Interaction) -> None:
  90. # Halt on ignore list or games channel settings.
  91. if await check_ignore_interaction(self.bot.pg, interaction, games=True):
  92. return
  93. if await get_theft_skill(self.bot.pg, interaction.user.id < 1):
  94. await interaction.followup.send("You do not have the `Theft` skill.")
  95. return
  96. victim_id = get_random_player(self.bot.pg)
  97. await interaction.send_message(f"You have decided to rob{self.bot.get_user(victim_id)}, unfortunately crime has not been invited yet.")
  98. @app_commands.command(
  99. name="dice",
  100. description="Simulate dice rolls.",
  101. )
  102. async def dice(self, interaction: discord.Interaction, amount: Optional[int], sides: Optional[int]) -> None:
  103. # Keep interaction alive longer then 3 seconds to give the bot time to respond.
  104. await interaction.response.defer()
  105. print(f"Extras: {interaction.extras}")
  106. print(f"Message: {interaction.message}")
  107. print(f"Data: {interaction.data}")
  108. # Halt on ignore list or games channel settings.
  109. if await check_ignore(self.bot.pg, interaction, games=True):
  110. return
  111. if not amount:
  112. amount = 2
  113. if not sides:
  114. sides = 6
  115. if amount < 1:
  116. await interaction.followup.send("You want me to roll less than one die? How!?")
  117. elif amount > 25:
  118. await interaction.followup.send("I can not hold so many dice at one time.")
  119. elif sides < 2:
  120. await interaction.followup.send("A die has physical minimum of 2 sides. Don't ask for impossible objects.")
  121. elif sides > 256:
  122. await interaction.followup.send("My tiny hands can not handle such large dice. Even if both are virtual.")
  123. else:
  124. embed = discord.Embed(title = "Dice roll", description=f"Rolling {amount} dice, with {sides} sides.")
  125. while amount > 0:
  126. embed.insert_field_at(0, name=f"Die {amount}", value=random.randint(1, sides), inline=True)
  127. amount -= 1
  128. await interaction.followup.send(embed=embed)
  129. @app_commands.command(
  130. name="8ball",
  131. description="Ask the magic 8-ball.",
  132. )
  133. async def eightball(self, interaction: discord.Interaction, question: str = None) -> None:
  134. theorem = f"{interaction.user.mention} asked: `{question}`"
  135. await interaction.response.send_message(theorem)
  136. # Halt on ignore list or games channel settings.
  137. if await check_ignore(self.bot.pg, interaction, games=True):
  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 interaction.edit_original_response(content=f"{theorem}\n**{random.choice(messages)}**")