From 0c4b6e6e8c69709e638cc28d1ba338c5388f342c Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Sun, 28 Nov 2021 18:41:56 +0000 Subject: Add commands --- commands/basic_games.py | 148 ++++++++++++++++++++++++++++++++++++++++++++---- commands/economy.py | 42 ++++++++++++-- commands/inventory.py | 101 +++++++++++++++++++++++++++++++++ commands/rank.py | 51 +++++++++++++++++ 4 files changed, 327 insertions(+), 15 deletions(-) create mode 100644 commands/inventory.py create mode 100644 commands/rank.py diff --git a/commands/basic_games.py b/commands/basic_games.py index 0b594ba..bad02c6 100644 --- a/commands/basic_games.py +++ b/commands/basic_games.py @@ -41,15 +41,17 @@ class BasicGames(Cog): async def coin_flip(self, ctx: SlashContext, num_coins: int, choice: int): user = User.get(User.discord_id == ctx.author_id) - await ctx.send(content=f"{user.username} is betting {num_coins} coins on {choice}") - result = random.randrange(0, 1) + await ctx.send( + content=f"{user.username} is betting {num_coins} {config['currency']} on {choice}" + ) choices = ["Heads", "Tails"] - await ctx.send(content=f"Coin flip lands on {choices[result]}") - if choice == choices[result]: - await ctx.send(content=f"You win {num_coins * 2} coins!") + result = random.choice(choices) + await ctx.send(content=f"Coin flip lands on {result}") + if choice == result: + await ctx.send(content=f"You win {num_coins * 2} {config['currency']}!") user.add_coins(num_coins * 2) else: - await ctx.send(content=f"You lose {num_coins} coins :(") + await ctx.send(content=f"You lose {num_coins} {config['currency']} :(") user.remove_coins(num_coins) user.save() @@ -58,12 +60,138 @@ class BasicGames(Cog): name="dice", description="Roll some dice.", guild_ids=[config["discord_server_id"]], + options=[ + create_option( + name="num_coins", + description="coins to bet", + option_type=4, + required=True, + ), + ], ) - async def dice(self, ctx: SlashContext): - print("dice") + async def dice(self, ctx: SlashContext, num_coins: int): user = User.get(User.discord_id == ctx.author_id) - result = random.randrange(1, 6) - await ctx.send(content=f"{user.display_name}, you roll a {result}") + if user.currency < num_coins: + await ctx.send( + content=f"{user.display_name}, you don't have enough {config['currency']} for that bet" + ) + return False + + results = [ + random.randint(1, 6), + random.randint(1, 6), + random.randint(1, 6), + random.randint(1, 6), + ] + await ctx.send(content=f"{user.display_name}, you roll a {results[0]} and a {results[1]}") + + user_total = results[0] + results[1] + bot_total = results[2] + results[3] + if user_total == 12: + winnings = num_coins * 2 + await ctx.channel.send(content=f"Double 6! You win {winnings} {config['currency']}!") + user.add_coins(winnings) + elif results[0] == results[1]: + winnings = num_coins * 2 + await ctx.channel.send( + content=f"You roll a double! You win {winnings} {config['currency']}!" + ) + user.add_coins(winnings) + else: + await ctx.channel.send( + content=f"{user.display_name}, I rolled a {results[2]} and a {results[3]}" + ) + if user_total > bot_total: + await ctx.channel.send(content=f"You win {num_coins} {config['currency']}!") + user.add_coins(num_coins) + elif user_total == bot_total: + await ctx.channel.send(content="It's a draw! Have your money back...") + else: + await ctx.channel.send(content=f"You lose {num_coins} {config['currency']} :(") + user.remove_coins(num_coins) + user.save() + + @cog_ext.cog_slash( + name="slots", + description="Pull the slot machine.", + guild_ids=[config["discord_server_id"]], + ) + async def slots(self, ctx: SlashContext): + user = User.get(User.discord_id == ctx.author_id) + cost = 100 + if user.currency < cost: + await ctx.send( + content=f"{user.display_name}, you don't have enough {config['currency']}, it costs 100 {config['currency']} to play" + ) + return False + + await ctx.send( + content=f"{user.display_name}, you put {cost} {config['currency']} in the machine and pull the handle..." + ) + user.remove_coins(cost) + + results = [ + random.randint(0, 5), + random.randint(0, 5), + random.randint(0, 5), + ] + results_top = [ + random.randint(0, 5), + random.randint(0, 5), + random.randint(0, 5), + ] + results_bottom = [ + random.randint(0, 5), + random.randint(0, 5), + random.randint(0, 5), + ] + + result_text = "" + e = list(config["slots"].values()) + winnings = 0 + + for r in results_top: + result_text += e[r]["dark"] + result_text += "\n" + + for r in results: + result_text += e[r]["emoji"] + winnings += e[r]["reward_single"] + result_text += "\n" + + double = False + triple = False + if results[0] == results[1] or results[0] == results[2]: + double = True + winnings += e[results[0]]["reward_double"] + elif results[1] == results[2]: + double = True + winnings += e[results[1]]["reward_double"] + elif results[0] == results[1] and results[0] == results[2]: + triple = True + winnings += e[results[0]]["reward_triple"] + + for r in results_bottom: + result_text += e[r]["dark"] + result_text += "\n" + + await ctx.channel.send(content=result_text) + + if winnings == 0: + await ctx.channel.send(content=f"No match :( You lose {cost} {config['currency']}") + user.remove_coins(cost) + else: + if triple: + await ctx.channel.send(content=f"JACKPOT! You win {winnings} {config['currency']}") + elif double: + await ctx.channel.send( + content=f"Ding ding! You win {winnings} {config['currency']}" + ) + else: + await ctx.channel.send(content=f"You win {winnings} {config['currency']}") + user.add_coins(winnings) + + user.save() def setup(bot): diff --git a/commands/economy.py b/commands/economy.py index 1960827..a2178c4 100644 --- a/commands/economy.py +++ b/commands/economy.py @@ -2,6 +2,7 @@ from discord.ext.commands import Bot, Cog from discord_slash import cog_ext, SlashContext from base import config from models.user import User +import datetime import random import yaml @@ -22,10 +23,26 @@ class Economy(Cog): ) async def daily(self, ctx: SlashContext): user = User.get(User.discord_id == ctx.author_id) - num_coins = random.randrange(50, 100) - user.add_coins(num_coins) - user.save() - await ctx.send(content="Here have some coins") + time_now = datetime.datetime.utcnow() + day_ago = time_now - datetime.timedelta(hours=24) + if user.daily_gained_time is None: + user.daily_gained_time = day_ago + remaining = user.daily_gained_time - day_ago + print( + f"now: {time_now}, day: {day_ago}, user: {user.exp_gained_time}, remaining: {remaining}" + ) + if user.daily_gained_time is None or user.daily_gained_time <= day_ago: + num_coins = random.randrange(50, 100) + user.daily_gained_time = time_now + user.add_coins(num_coins) + user.save() + await ctx.send( + content=f"Here you go {user.display_name}, have {num_coins} {config['currency']}" + ) + else: + await ctx.send( + content=f"No {config['currency']} for you {user.display_name}, come back in {remaining.seconds // 3600} hours, {(remaining.seconds % 3600) // 60 } minutes." + ) @cog_ext.cog_slash( name="coins", @@ -34,7 +51,22 @@ class Economy(Cog): ) async def coins(self, ctx: SlashContext): user = User.get(User.discord_id == ctx.author_id) - await ctx.send(content=f"{user.display_name}, you have {user.currency} coins") + await ctx.send( + content=f"{user.display_name}, you have {user.currency} {config['currency']}" + ) + + @cog_ext.cog_slash( + name="leaderboard", + description="Show the currency leaderboard", + guild_ids=[config["discord_server_id"]], + ) + async def leaderboard(self, ctx: SlashContext): + users = User.select().order_by(User.currency.desc()).limit(10) + rank_text = "" + for user in users: + rank_text += f"{user.display_name}: {user.currency} {config['currency']} \n" + + await ctx.send(content=rank_text) def setup(bot): diff --git a/commands/inventory.py b/commands/inventory.py new file mode 100644 index 0000000..1f81d71 --- /dev/null +++ b/commands/inventory.py @@ -0,0 +1,101 @@ +from discord.ext.commands import Bot, Cog +from discord_slash import cog_ext, SlashContext +from discord_slash.utils.manage_commands import create_option, create_choice +from base import config +from models.user import User +from models.level import Level +from models.item import Item +from models.inventory import Inventory +from peewee import DoesNotExist +import random +import yaml + + +class InventoryCmd(Cog): + def __init__(self, bot: Bot): + self.bot = bot + with open("config.yaml", "r") as yamlfile: + config = yaml.load(yamlfile, Loader=yaml.CLoader) + print("Read config successful") + self.config = config + print(config["discord_server_id"]) + + @cog_ext.cog_slash( + name="inventory", + description="See your inventory", + guild_ids=[config["discord_server_id"]], + ) + async def inventory(self, ctx: SlashContext): + user = User.get(User.discord_id == ctx.author_id) + inventory = Inventory.select().join(Item).limit(100).where(Inventory.user == user.id) + print(inventory) + shop_text = "" + for inventory_item in inventory: + shop_text += ( + f"#{inventory_item.item.id}: {inventory_item.item.name} x{inventory_item.stock} \n" + ) + await ctx.send(content=shop_text) + + @cog_ext.cog_slash( + name="use", + description="Use an item", + guild_ids=[config["discord_server_id"]], + options=[ + create_option( + name="item", + description="Item to use", + option_type=3, + required=True, + ), + create_option( + name="target", + description="User to use the item on", + option_type=6, + required=False, + ), + ], + ) + async def use(self, ctx: SlashContext, item: str, target=None): + user = User.get(User.discord_id == ctx.author_id) + + try: + if is_number(item): + using_item = Item.get(Item.id == item) + else: + using_item = Item.get(Item.name == item) + except DoesNotExist: + await ctx.send(content=f"Item {item} not found. Try using the item number") + + try: + print(f"user: {user}, item: {using_item}") + inventory_item = Inventory.get(user=user, item=using_item) + except DoesNotExist: + await ctx.send(content=f"You don't own any {item}!") + if inventory_item.stock > 0: + inventory_item.stock -= 1 + inventory_item.save() + use_text = using_item.text + use_text = use_text.replace("{u}", ctx.author.mention) + if target is not None: + use_text = use_text.replace("{t}", target.mention) + message = await ctx.send(content=f"Solarus used *{using_item.name}*!") + await ctx.send(content=f"*{use_text}*") + if using_item.reward_rank is not None: + print(using_item.reward_rank) + role = ctx.author.guild.get_role(int(using_item.reward_rank)) + await ctx.author.add_roles(role) + else: + await ctx.send(content=f"You don't own any more {using_item.name}!") + + +def is_number(s): + try: + int(s) + return True + except ValueError: + return False + + +def setup(bot): + bot.add_cog(InventoryCmd(bot)) + print("Loaded Inventory") diff --git a/commands/rank.py b/commands/rank.py new file mode 100644 index 0000000..a16b1c2 --- /dev/null +++ b/commands/rank.py @@ -0,0 +1,51 @@ +from discord.ext.commands import Bot, Cog +from discord_slash import cog_ext, SlashContext +from base import config +from models.user import User +from models.level import Level +import random +import yaml + + +class Rank(Cog): + def __init__(self, bot: Bot): + self.bot = bot + with open("config.yaml", "r") as yamlfile: + config = yaml.load(yamlfile, Loader=yaml.CLoader) + print("Read config successful") + self.config = config + print(config["discord_server_id"]) + + @cog_ext.cog_slash( + name="rank", + description="Show your rank.", + guild_ids=[config["discord_server_id"]], + ) + async def rank(self, ctx: SlashContext): + user = User.get(User.discord_id == ctx.author_id) + next_level = Level.get(Level.level == user.level + 1) + + await ctx.send( + content=f"Hey {user.display_name}, you are level {user.level}. You're at {user.exp}/{next_level.exp_required}" + ) + + @cog_ext.cog_slash( + name="ranks", + description="Show the rank leaderboard", + guild_ids=[config["discord_server_id"]], + ) + async def ranks(self, ctx: SlashContext): + users = User.select().order_by(User.exp.desc()).limit(10) + rank_text = "" + for user in users: + next_level = Level.get(user.level + 1) + rank_text += ( + f"{user.display_name}: Level {user.level}, {user.exp}/{next_level.exp_required} \n" + ) + + await ctx.send(content=rank_text) + + +def setup(bot): + bot.add_cog(Rank(bot)) + print("Loaded Rank") -- cgit v1.2.3