idlerpg.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import discord
  2. from discord.ext import commands
  3. from typing import Optional
  4. from query import user as userquery
  5. from common.settings import check_ignore
  6. class Idlerpg(commands.Cog):
  7. def __init__(self, bot): # Special method that is called when the cog is loaded
  8. self.bot = bot
  9. @commands.slash_command(
  10. name = "level",
  11. description="Check the level for a player.",
  12. brief="Get player level",
  13. help="View game level of player."
  14. )
  15. async def level(self, ctx: commands.Context, user: Optional[discord.User]):
  16. # Halt on ignore list or games channel settings.
  17. if await check_ignore(self.bot.pg, ctx.author, ctx.channel):
  18. return
  19. if not user:
  20. user = ctx.author
  21. level = await userquery.get_level(self.bot.pg, user.id)
  22. if level == 0:
  23. if ctx.author == user:
  24. await ctx.respond(f"You are not playing, join the game with `/levelup`")
  25. else:
  26. await ctx.respond(f"`{user}` is not playing.")
  27. else:
  28. xp_spent, total_xp = await userquery.get_xp(self.bot.pg, user.id)
  29. ability_points_spent = await userquery.get_ability_points_spent(self.bot.pg, user.id)
  30. coin = await userquery.get_coin(self.bot.pg, user.id)
  31. karma = await userquery.get_karma(self.bot.pg, user.id)
  32. if ctx.author == user:
  33. await ctx.respond(
  34. f"You rank at level **{level}**. (exp **{xp_spent}**/{total_xp} | abp **{ability_points_spent}**/{level * 3} | coin **{coin}** | karma **{karma}**)")
  35. else:
  36. await ctx.respond(
  37. f"`{user}` ranks at level **{level}**. (**{xp_spent}**/{total_xp} | abp **{ability_points_spent}**/{level * 3}) | coin **{coin}** | karma **{karma}**")
  38. @commands.slash_command(
  39. description="Check the experience points for a player.",
  40. brief="Get player xp",
  41. help="View amount of XP a game player has."
  42. )
  43. async def xp(self, ctx: commands.Context, user: Optional[discord.User]):
  44. # Halt on ignore list or games channel settings.
  45. if await check_ignore(self.bot.pg, ctx.author, ctx.channel):
  46. return
  47. if not user:
  48. xp_spent, total_xp = await userquery.get_xp(self.bot.pg, ctx.author.id)
  49. level = await userquery.get_level(self.bot.pg, ctx.author.id)
  50. threshold = (level + 1) * 50 + xp_spent
  51. if threshold < total_xp - xp_spent:
  52. await ctx.respond(f"You have spent {xp_spent} experience points of your {total_xp} total and can gain 3 ability points for {threshold} xp.")
  53. else:
  54. await ctx.respond(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`.")
  55. else:
  56. xp_spent, total_xp = await userquery.get_xp(self.bot.pg, user.id)
  57. level = await userquery.get_level(self.bot.pg, user.id)
  58. threshold = (level + 1) * 50 + xp_spent
  59. if threshold < total_xp - xp_spent:
  60. await ctx.respond(f"`{user}` has spent {xp_spent} of {total_xp} experience points and can level up for {threshold} xp.")
  61. else:
  62. await ctx.respond(f"`{user}` has spent {xp_spent} of {total_xp} experience points and requires {threshold - (total_xp - xp_spent)} xp to level up.")
  63. @commands.slash_command(
  64. description="Attempt to gain a level.",
  65. brief="Level up",
  66. help="Try to rank up a level in the game by spending XP."
  67. )
  68. async def levelup(self, ctx: commands.Context):
  69. # Halt on ignore list or games channel settings.
  70. if await check_ignore(self.bot.pg, ctx.author, ctx.channel):
  71. return
  72. xp_spent, total_xp = await userquery.get_xp(self.bot.pg, ctx.author.id)
  73. xp_available = total_xp - xp_spent
  74. level = await userquery.get_level(self.bot.pg, ctx.author.id)
  75. threshold = (level + 1) * 50 + xp_spent
  76. if xp_available < threshold:
  77. await ctx.respond(f"Not yet, you require {threshold - xp_available} more XP to level up.")
  78. else:
  79. await userquery.level_up(self.bot.pg, ctx.author.id, threshold)
  80. await ctx.respond(f"You have gained three ability points climbed the ranks for {threshold} XP, leaving you {xp_available - threshold} remaining.")
  81. @commands.slash_command(
  82. description="Rob another player",
  83. brief="Rob a player",
  84. help="Pursuit a robbery."
  85. )
  86. async def rob(self, ctx: commands.Context):
  87. # Halt on ignore list or games channel settings.
  88. if await check_ignore(self.bot.pg, ctx.author, ctx.channel):
  89. return
  90. if await userquery.get_theft_skill(self.bot.pg, ctx.author.id < 1):
  91. await ctx.respond("You do not have the `Theft` skill.")
  92. return
  93. victim_id = userquery.get_random_player(self.bot.pg)
  94. await ctx.respond(f"You have decided to rob{self.bot.get_user(victim_id)}, unfortunately crime has not been invited yet.")
  95. def setup(bot): # Called by Pycord to setup the cog
  96. bot.add_cog(Idlerpg(bot)) # Add the cog to the bot