From d529daaa27e7f3d7b4e712bee26215f60638a122 Mon Sep 17 00:00:00 2001 From: Harsha Raghu Date: Mon, 6 Dec 2021 19:14:47 +0530 Subject: [PATCH] [Guild_Admin] set_coins Admin command --- bot/modules/guild_admin/__init__.py | 1 + bot/modules/guild_admin/economy/__init__.py | 3 + bot/modules/guild_admin/economy/set_coins.py | 104 +++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 bot/modules/guild_admin/economy/__init__.py create mode 100644 bot/modules/guild_admin/economy/set_coins.py diff --git a/bot/modules/guild_admin/__init__.py b/bot/modules/guild_admin/__init__.py index 74d5644d..2c4f3922 100644 --- a/bot/modules/guild_admin/__init__.py +++ b/bot/modules/guild_admin/__init__.py @@ -4,3 +4,4 @@ from . import guild_config from . import statreset from . import new_members from . import reaction_roles +from . import economy diff --git a/bot/modules/guild_admin/economy/__init__.py b/bot/modules/guild_admin/economy/__init__.py new file mode 100644 index 00000000..2714bcdb --- /dev/null +++ b/bot/modules/guild_admin/economy/__init__.py @@ -0,0 +1,3 @@ +from ..module import module + +from . import set_coins \ No newline at end of file diff --git a/bot/modules/guild_admin/economy/set_coins.py b/bot/modules/guild_admin/economy/set_coins.py new file mode 100644 index 00000000..d74651a9 --- /dev/null +++ b/bot/modules/guild_admin/economy/set_coins.py @@ -0,0 +1,104 @@ +import discord +import datetime +from cmdClient.checks import in_guild + +from settings import GuildSettings +from core import Lion + +from ..module import module + +POSTGRES_INT_MAX = 2147483647 + +@module.cmd( + "set_coins", + group="Guild Admin", + desc="Set coins on a member." +) +@in_guild() +async def cmd_set(ctx): + """ + Usage``: + {prefix}set_coins + Description: + Sets the given number of coins on the mentioned user. + If a number greater than 0 is mentioned, will add coins. + If a number less than 0 is mentioned, will remove coins. + Note: LionCoins on a member cannot be negative. + Example: + {prefix}set_coins {ctx.author.mention} 100 + {prefix}set_coins {ctx.author.mention} -100 + """ + # Extract target and amount + # Handle a slightly more flexible input than stated + splits = ctx.args.split() + digits = [isNumber(split) for split in splits[:2]] + mentions = ctx.msg.mentions + if len(splits) < 2 or not any(digits) or not (all(digits) or mentions): + return await _send_usage(ctx) + + if all(digits): + # Both are digits, hopefully one is a member id, and one is an amount. + target, amount = ctx.guild.get_member(int(splits[0])), int(splits[1]) + if not target: + amount, target = int(splits[0]), ctx.guild.get_member(int(splits[1])) + if not target: + return await _send_usage(ctx) + elif digits[0]: + amount, target = int(splits[0]), mentions[0] + elif digits[1]: + target, amount = mentions[0], int(splits[1]) + + # Fetch the associated lion + target_lion = Lion.fetch(ctx.guild.id, target.id) + + # Check sanity conditions + if target == ctx.client.user: + return await ctx.embed_reply("Thanks, but Ari looks after all my needs!") + if target.bot: + return await ctx.embed_reply("We are still waiting for {} to open an account.".format(target.mention)) + + # Finally, send the amount and the ack message + # Postgres `coins` column is `integer`, sanity check postgres int limits - which are smalled than python int range + target_coins_to_set = target_lion.coins + amount + if target_coins_to_set >= 0 and target_coins_to_set <= POSTGRES_INT_MAX: + target_lion.addCoins(amount) + elif target_coins_to_set < 0: + target_coins_to_set = -target_lion.coins # Coins cannot go -ve, cap to 0 + target_lion.addCoins(target_coins_to_set) + target_coins_to_set = 0 + else: + return await ctx.embed_reply("Member coins cannot be more than {}".format(POSTGRES_INT_MAX)) + + embed = discord.Embed( + title="Funds Set", + description="You have set LionCoins on {} to **{}**!".format(target.mention,target_coins_to_set), + colour=discord.Colour.orange(), + timestamp=datetime.datetime.utcnow() + ).set_footer(text=str(ctx.author), icon_url=ctx.author.avatar_url) + + await ctx.reply(embed=embed, reference=ctx.msg) + GuildSettings(ctx.guild.id).event_log.log( + "{} set {}'s LionCoins to`{}`.".format( + ctx.author.mention, + target.mention, + target_coins_to_set + ), + title="Funds Set" + ) + +def isNumber(var): + try: + return isinstance(int(var), int) + except: + return False + +async def _send_usage(ctx): + return await ctx.error_reply( + "**Usage:** `{prefix}set_coins `\n" + "**Example:**\n" + " {prefix}set_coins {ctx.author.mention} 100\n" + " {prefix}set_coins {ctx.author.mention} -100".format( + prefix=ctx.best_prefix, + ctx=ctx + ) + )