diff options
Diffstat (limited to 'commands')
-rw-r--r-- | commands/basic_games.py | 204 | ||||
-rw-r--r-- | commands/economy.py | 57 | ||||
-rw-r--r-- | commands/inventory.py | 101 | ||||
-rw-r--r-- | commands/rank.py | 51 | ||||
-rw-r--r-- | commands/shop.py | 88 |
5 files changed, 483 insertions, 18 deletions
diff --git a/commands/basic_games.py b/commands/basic_games.py index 0b594ba..66524cd 100644 --- a/commands/basic_games.py +++ b/commands/basic_games.py @@ -1,5 +1,6 @@ from discord.ext.commands import Bot, Cog from discord_slash import cog_ext, SlashContext +from discord import Embed from discord_slash.utils.manage_commands import create_option, create_choice from base import config from models.user import User @@ -41,29 +42,206 @@ 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) - 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!") - user.add_coins(num_coins * 2) - else: - await ctx.send(content=f"You lose {num_coins} coins :(") + await ctx.send( + content=f"{user.username} is betting {num_coins} {config['currency']} on {choice}" + ) + result = random.randint(0, 200) + if result <= 110: + if choice == "Heads": + await ctx.send(content="Coin flip lands on Tails") + else: + await ctx.send(content="Coin flip lands on Heads") + + embed = Embed(description=f"{ctx.author.mention} loses!", colour=0xFF0000) + embed.add_field(name="Lost:", value=f"{num_coins} {config['currency']}") user.remove_coins(num_coins) + elif result > 110 and result < 199: + if choice == "Heads": + await ctx.send(content="Coin flip lands on Heads") + else: + await ctx.send(content="Coin flip lands on Tails") + embed = Embed(description=f"{ctx.author.mention} wins!", colour=0x00FF00) + embed.add_field(name="Prize:", value=f"{num_coins} {config['currency']}") + user.add_coins(num_coins) + elif result >= 199: + winnings = num_coins * 2 + embed = Embed( + description=f"The coin lands on it's side!{ctx.author.mention} wins double!", + colour=0x00FF00, + ) + embed.add_field(name="Prize:", value=f"{winnings} {config['currency']}") + user.add_coins(winnings) + await ctx.send(embed=embed) user.save() @cog_ext.cog_slash( 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, num_coins: int): + user = User.get(User.discord_id == ctx.author_id) + 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 * 3 + + embed = Embed( + description=f"{ctx.author.mention} rolls a double 6!!! You win!", colour=0x00FF00 + ) + embed.add_field(name="Prize:", value=f"{winnings} {config['currency']}") + user.add_coins(winnings) + elif results[0] == results[1]: + winnings = num_coins * 2 + embed = Embed( + description=f"{ctx.author.mention} rolls a double!! You win!", colour=0x00FF00 + ) + embed.add_field(name="Prize:", value=f"{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: + embed = Embed(description=f"{ctx.author.mention} wins!", colour=0x00FF00) + embed.add_field(name="Prize:", value=f"{num_coins} {config['currency']}") + user.add_coins(num_coins) + elif user_total == bot_total: + embed = Embed( + description=f"{ctx.author.mention}, it's a draw. Have your money back...", + colour=0xFFFF00, + ) + else: + embed = Embed( + description=f"{ctx.author.mention} loses the dice roll :(", + colour=0xFF0000, + ) + embed.add_field(name="Lost:", value=f"{num_coins} {config['currency']}") + user.remove_coins(num_coins) + await ctx.channel.send(embed=embed) + user.save() + + @cog_ext.cog_slash( + name="slots", + description="Pull the slot machine.", + guild_ids=[config["discord_server_id"]], ) - async def dice(self, ctx: SlashContext): - print("dice") + async def slots(self, ctx: SlashContext): 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}") + cost = 10 + 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) + + slots = config["slots"] + + e = [] + for slot in slots: + for i in range(slot["rarity"]): + e.append(slot) + total = len(e) - 1 + results = [ + e[random.randint(0, total)], + e[random.randint(0, total)], + e[random.randint(0, total)], + ] + results_top = [ + e[random.randint(0, total)], + e[random.randint(0, total)], + e[random.randint(0, total)], + ] + results_bottom = [ + e[random.randint(0, total)], + e[random.randint(0, total)], + e[random.randint(0, total)], + ] + + result_text = "" + # e = list(config["slots"].values()) + winnings = 0 + + for r in results_top: + result_text += r["dark"] + result_text += "\n" + + for r in results: + result_text += r["emoji"] + result_text += "\n" + + for r in results_bottom: + result_text += r["dark"] + result_text += "\n" + + double = False + triple = False + + if results[0]["name"] == results[1]["name"] and results[0]["name"] == results[2]["name"]: + triple = True + winnings += results[0]["reward_triple"] + elif results[0]["name"] == results[1]["name"] or results[0]["name"] == results[2]["name"]: + double = True + winnings += results[0]["reward_double"] + elif results[1]["name"] == results[2]["name"]: + double = True + winnings += results[1]["reward_double"] + else: + for r in results: + winnings += r["reward_single"] + + await ctx.channel.send(content=result_text) + + if winnings == 0: + embed = Embed(description=f"No match :( {ctx.author.mention} loses...", colour=0xFF0000) + embed.add_field(name="Lost:", value=f"{cost} {config['currency']}") + else: + if triple: + embed = Embed( + description=f"JACKPOT! {ctx.author.mention} wins with a triple!!!", + colour=0x00FF00, + ) + elif double: + embed = Embed( + description=f"Ding ding! {ctx.author.mention} wins with a double!!", + colour=0x00FF00, + ) + else: + embed = Embed(description=f"{ctx.author.mention} wins!", colour=0x00FF00) + + embed.add_field(name="Prize:", value=f"{winnings} {config['currency']}") + user.add_coins(winnings) + + await ctx.channel.send(embed=embed) + user.save() def setup(bot): diff --git a/commands/economy.py b/commands/economy.py index 1960827..720a00d 100644 --- a/commands/economy.py +++ b/commands/economy.py @@ -1,7 +1,9 @@ from discord.ext.commands import Bot, Cog from discord_slash import cog_ext, SlashContext +from discord import Embed from base import config from models.user import User +import datetime import random import yaml @@ -22,10 +24,30 @@ 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 = 10 + user.level * 2 + user.daily_gained_time = time_now + user.add_coins(num_coins) + user.save() + embed = Embed( + description=f"Here you go {ctx.author.mention}, have your daily coins", + colour=0x00FF00, + ) + embed.add_field(name="Gained:", value=f"{num_coins} {config['currency']}") + else: + embed = Embed( + description=f"No {config['currency']} for you {user.display_name}, come back in {remaining.seconds // 3600} hours, {(remaining.seconds % 3600) // 60 } minutes.", + colour=0xFF8800, + ) + await ctx.send(embed=embed) @cog_ext.cog_slash( name="coins", @@ -34,7 +56,32 @@ 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") + embed = Embed( + description=f"{ctx.author.mention}, here is your current balance:", + colour=0x0000FF, + ) + embed.add_field(name="Currency:", value=f"{user.currency} {config['currency']}") + await ctx.send(embed=embed) + + @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) + embed = Embed( + title="Currency Leaderboard", + colour=0x0000FF, + ) + i = 1 + for user in users: + embed.add_field( + name=f"#{i} {user.display_name}", value=f"{user.currency} {config['currency']}" + ) + i += 1 + + await ctx.send(embed=embed) 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") diff --git a/commands/shop.py b/commands/shop.py new file mode 100644 index 0000000..5c9bb9f --- /dev/null +++ b/commands/shop.py @@ -0,0 +1,88 @@ +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 Shop(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="shop", + description="Browse the shop.", + guild_ids=[config["discord_server_id"]], + ) + async def shop(self, ctx: SlashContext): + items = Item.select().limit(100) + shop_text = "" + for item in items: + shop_text += f"#{item.id} {item.name}: {item.price} {config['currency']} \n" + await ctx.send(content=shop_text) + + @cog_ext.cog_slash( + name="buy", + description="Buy from the shop", + guild_ids=[config["discord_server_id"]], + options=[ + create_option( + name="item", + description="Item to buy", + option_type=3, + required=True, + ), + ], + ) + async def buy(self, ctx: SlashContext, item: str): + user = User.get(User.discord_id == ctx.author_id) + + try: + if is_number(item): + buying_item = Item.get(Item.id == item) + else: + buying_item = Item.get(Item.name == item) + except DoesNotExist: + await ctx.send(content=f"Item {item} not found. Try using the item number") + + else: + if user.currency >= buying_item.price: + try: + inventory = Inventory.get(user=user, item=buying_item) + except DoesNotExist: + inventory = Inventory(user=user, item=buying_item, stock=0) + inventory.stock += 1 + user.currency -= buying_item.price + inventory.save() + user.save() + await ctx.send( + content=f"{user.display_name} bought the item: {buying_item.name} for {buying_item.price} {config['currency']}" + ) + else: + await ctx.send( + content=f"{buying_item.name} costs {buying_item.price} {config['currency']}. You can't afford that {user.display_name}!" + ) + + +def is_number(s): + try: + int(s) + return True + except ValueError: + return False + + +def setup(bot): + bot.add_cog(Shop(bot)) + print("Loaded Shop") |