def roll_logic(message): prefix_length = len((G.OPT.prefix + G.LOC.commands.roll.id)) + 2 out = tools.MsgBuilder() try: number = int(message.content[prefix_length:]) outcome = random.randint(1, number) # Check for achievements if number == 20 and outcome == 20: tools.update_user(message.author.id, stat="gotLucky", set=True) if number == 20 and outcome == 1: tools.update_user(message.author.id, stat="gotUnlucky", set=True) out.add(G.LOC.commands.roll.result.format(sides=number, result=outcome)) return out.parse() except ValueError: out.add( G.LOC.commands.roll.coercionError.format( (G.OPT.prefix + G.LOC.commands.roll.id))) return out.parse()
def __init__(self, user_id): """Find and make all items for a certain user. Every time we access a user's items we should do it through this class. Args: user_id: str Id of user to make items for. """ self.user_id = user_id if G.USR[user_id].inventory in [0, None]: # We never made any items for this user. tools.update_user(user_id=user_id, stat="inventory", set=munch.Munch()) if len(G.USR[user_id].inventory) == 0: self.is_empty = True else: self.is_empty = False self.content = [] for key, is_owned in G.USR[user_id].inventory.items(): if is_owned: self.content.append(Item(key))
def sell_item_logic(message): # Grab from message the name and id of the item user_id = str(message.author.id) inventory = Inventory(user_id) arg = str(message.content[len(G.OPT.prefix) + len(G.LOC.commands.sell.id) + 1:]) arg = arg.lower() out = tools.MsgBuilder() for key, item in G.LOC["items"].items(): # Search for the item in the localization file. if arg == item.name.lower(): identifier = key new_item = Item(identifier) break else: out.add(G.LOC.commands.sell.unknown) return out.parse() if identifier not in [x.id for x in inventory.content]: out.add(G.LOC.commands.sell.noitem.format(new_item.name)) return out.parse() if new_item.unsellable: out.add(G.LOC.commands.sell.unsellable.format(new_item.name)) return out.parse() # Remove the item inventory.remove_item(identifier) # Refund tokens tools.update_user(user_id, new_item.currency, increase=new_item.resell) out.add(G.LOC.commands.sell.success.format( new_item.name, tools.fn(new_item.resell), new_item.currency_name )) return out.parse()
async def on_message(message): """Handles parsing and responding to all incoming messages Milton can see.""" # Special checks on the message before parsing commands # Don't reply to yourself if message.author == client.user: return # Update locale for current guild G.update_loc(G.GLD[str(message.guild.id)].language) # Randomly throw out an insult, for fun if message.content.startswith(G.OPT.prefix) is False and\ G.OPT.insult_threshold is not False and\ message.author != client.user: if G.OPT.insult_threshold == random.randint(0, G.OPT.insult_threshold): await message.channel.send(tools.get_random_line(G.LOC.randomInsult_path)) # Don't compute further if it isn't directed to Milton. if message.content.startswith(G.OPT.prefix) is False: return # Count the number of times this person typed a command. tools.update_user(user_id=message.author.id, stat="commandCount", increase=1) # Update total joules (remove in a while?): if G.USR[str(message.author.id)].lifetime_joules == 0 and \ G.USR[str(message.author.id)].joules > 0: stats = idle.make_all_stats(str(message.author.id)) min_joules = G.USR[str(message.author.id)].joules for stat in stats.values(): min_joules += stat.recalculate_price(-1) tools.update_user(str(message.author.id), stat="lifetime_joules", set=min_joules) # Run normal commands for command in G.COMMANDS: if command.permission(message) is True: if command.where == "channel": for string in command.logic(message): await message.channel.send(string) elif command.where == "user": if message.author.dm_channel is None: await message.author.create_dm() for string in command.logic(message): await message.author.dm_channel.send(string) await message.delete() # Achievements that contain strings: if message.content.startswith(G.OPT.prefix + "^^vv<><>ba"): tools.update_user(str(message.author.id), "konami", set=True) await message.delete() if message.content.startswith(G.OPT.prefix + "cheat"): tools.update_user(str(message.author.id), "cheat", set=True) await message.delete() # Check Achievements achieve_intro = True for achieve in G.ACHIEVES: if achieve.status == "legacy": continue if G.USR[str(message.author.id)].epoch < achieve.epoch: continue if achieve.check_trigger(str(message.author.id)) is True: out = tools.MsgBuilder() if achieve_intro: out.add(G.LOC.msg.on_award) achieve_intro = False out.add(achieve.award(message.author.id)) for string in out.parse(): await message.channel.send(string)
def random_fact_logic(message): out = tools.MsgBuilder() tools.update_user(message.author.id, stat="factCount", increase=1) tools.save_users() out.add(tools.get_random_line(G.LOC.random_path)) return out.parse()
def buy_item_logic(message): """Handles buying an item and generates a string to push to chat.""" user_id = str(message.author.id) inventory = Inventory(user_id) arg = str(message.content[len(G.OPT.prefix) + len(G.LOC.commands.buy.id) + 1:]) arg = arg.lower() out = tools.MsgBuilder() for key, item in G.LOC["items"].items(): # Search for the item in the localization file. if arg == item.name.lower() and G.IDLE["items"][key].epoch <= G.USR[user_id].epoch: identifier = key new_item = Item(identifier) break else: out.add(G.LOC.commands.buy.unknown) return out.parse() for item in inventory.content: # Cannot buy an already-owned item. if item.id == identifier: out.add(G.LOC.commands.buy.already_bought.format(arg)) return out.parse() if G.IDLE["items"][identifier].builds_from is not None: # Cannot buy if we don't have the required items. current_ids = [item.id for item in inventory.content] required_items = set(G.IDLE["items"][identifier].builds_from) if required_items.issubset(current_ids) is False: out.add(G.LOC.commands.buy.no_requirements) return out.parse() else: required_items = set() unsellables = [x for x in inventory.content if x.unsellable is True] if len(inventory.content) >= (G.IDLE.max_items + len(required_items) + len(unsellables)): # Cannot buy an item if inventory is full out.add(G.LOC.commands.buy.nospace.format(G.IDLE.max_items)) return out.parse() if G.USR[user_id][new_item.currency] <= new_item.cost: # Cannot buy if we don't have enough tokens out.add(G.LOC.commands.buy.not_enough.format( new_item.currency_name, tools.fn(G.USR[user_id][item.currency]), tools.fn(new_item.cost) )) return out.parse() # If we get here, all requirements to buy the item are fulfilled tools.update_user(user_id=user_id, stat=new_item.currency, increase=-new_item.cost) inventory.add_item(new_item.id) # Update epoch if key items are bought if inventory.contains("joule_condenser"): tools.update_user(user_id, stat="epoch", set=1) if G.IDLE["items"][identifier].builds_from is not None: # Remove required items for item in inventory.content: if item.id in G.IDLE["items"][identifier].builds_from: inventory.remove_item(item.id) inventory.update_content() # Probably useless out.add(G.LOC.commands.buy.success.format( new_item.name, tools.fn(new_item.cost), new_item.currency_name )) return out.parse()