def help_achieve_logic(message): user_info = G.USR[str(message.author.id)] help_msg = tools.MsgBuilder() help_msg.add(G.LOC.commands.help_achieve.intro) i = 0 for key, achieve in G.LOC.achievements.items(): # TODO: This is rather ugly. achievement = [x for x in G.ACHIEVES if x.id == key][0] if achievement.status == "legacy" and user_info[key] in [False, 0]: # We ignore legacy achievements that have not been unlocked by the user. continue if achieve.include_help or user_info[key]: if user_info[key]: help_msg.add( G.LOC.msg.format_yes.format( G.LOC.achievements[key].name, G.LOC.achievements[key].condition)) else: help_msg.add( G.LOC.msg.format_no.format( G.LOC.achievements[key].name, G.LOC.achievements[key].condition)) if achieve.include_help is False and user_info[key] in [False, 0]: i += 1 help_msg.add(G.LOC.msg.secret_achieves.format(i)) return help_msg.parse()
def help_logic(message): out = tools.MsgBuilder() out.add(G.LOC.msg.help) for command in G.LOC.commands.values(): if command.showHelp is True and "general" in command.category: out.add("**" + G.OPT.prefix + command.id + "**:\n\t" + command.help) return out.parse()
def short_help_achieve_logic(message): user_info = G.USR[str(message.author.id)] help_msg = tools.MsgBuilder() help_msg.add(G.LOC.commands.help_achieve_short.intro) for key, achieve in G.LOC.achievements.items(): if user_info[key]: help_msg.append(G.LOC.msg.format_short.format( G.LOC.achievements[key].name), sep=" ") return help_msg.parse()
def set_notification_channel_logic(message): out = tools.MsgBuilder() if G.GLD[str(message.author.guild.id )].notification_channel != message.channel.id: G.GLD[str( message.author.guild.id)].notification_channel = message.channel.id tools.save_guilds() out.add(G.LOC.commands.setnotification.notificationsenabled) return out.parse() else: G.GLD[str(message.author.guild.id)].notification_channel = 0 tools.save_guilds() out.add(G.LOC.commands.setnotification.notificationsdisabled) return out.parse()
def change_language_logic(message): prefix_length = len((G.OPT.prefix + G.LOC.commands.changeLang.id)) + 1 query = message.content[prefix_length:(prefix_length + 2)].lower() available_locales = G.GLOC.keys() out = tools.MsgBuilder() if query in available_locales: G.GLD[str(message.guild.id)].language = query tools.save(G.OPT.guilds_path, G.GLD) G.update_loc(G.GLOC[G.GLD[str(message.guild.id)].language]) out.add(G.LOC.commands.changeLang.success.format(query.upper())) return out.parse() else: locales = " ".join(available_locales).upper() out.add(G.LOC.commands.changeLang.error.format(query, locales)) return out.parse()
def show_shop_logic(message): """Show the item shop Should automatically grab new items as they are in the localization files. """ out = tools.MsgBuilder() out.add(G.LOC.commands.shop.intro) user_id = str(message.author.id) for identifier, item in G.LOC["items"].items(): item_object = Item(identifier) if G.USR[user_id].epoch >= G.IDLE["items"][identifier].epoch: out.add(G.LOC.commands.shop.item_template.format( item.name, item.info, G.IDLE["items"][identifier].cost, item_object.currency_name )) return out.parse()
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 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 user_info_logic(message): user_id = str(message.author.id) stats = make_all_stats(user_id) # Give user information strings = G.LOC.commands.userInfo out = tools.MsgBuilder() bonus_tokens = 0 inventory = Inventory(user_id) if G.USR[user_id].times_ascended <= len(G.IDLE.ascension.bonus): bonus_tokens = G.IDLE.ascension.bonus[G.USR[user_id].times_ascended] # Introduction to help message out.add(strings.info) # Information about joules and lifetime joules out.add( strings.joules.format( tools.fn(G.USR[user_id].joules, decimals=2), tools.fn(G.USR[user_id].lifetime_joules, decimals=2))) # Information about tokens, token effects, and tokens gained on ascension. out.add( strings.tokens.format( tools.fn(G.USR[user_id].tokens, decimals=1), tools.fn(1 + (log(1 + G.USR[user_id].tokens, 10))), tools.fn(tokens_from_joules(G.USR[user_id].lifetime_joules)), tools.fn(bonus_tokens, decimals=1))) # Information about matter level if G.USR[user_id].epoch >= 1 and G.USR[user_id].matter > 0: out.add(strings.matterlevel.format(tools.fn(G.USR[user_id].matter))) # Information about lifetime statistics out.add( strings.lifetime.format(G.USR[user_id].times_ascended, tools.fn(G.USR[user_id].sacrificed_joules))) # Information about "production" stat out.add( strings.productionlevel.format( stats.production.stat_level, tools.fn(stats.production.value() * 60, decimals=1))) # Information about "time" stat out.add( strings.timelevel.format(stats.time.stat_level, tools.fn(stats.time.value()))) # Information about "attack" stat out.add( strings.attacklevel.format(stats.attack.stat_level, tools.fn(stats.attack.value()))) # Information about "commandCount" stat out.add( strings.commandCount.format(G.USR[str( message.author.id)].commandCount)) # Information about items in inventory. items = ", ".join( [G.LOC["items"][item.id].name for item in inventory.content]) items = items.rstrip() if items == "": out.add(strings.noitems) else: out.add(strings["items"].format(items)) # Information about achievements out.add( strings.achieves.format( tools.count_achieves(user_id), round(G.IDLE.harvest.achievebonus**tools.count_achieves(user_id), 2))) # Random funny line. out.add(("> " + tools.get_random_line(G.LOC.randomInfo_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()