async def get_config(message, args): guild = client.get_guild(util.safe_int(args.strip(), 0)) if guild is None: if message.guild is None: await message.channel.send("No configuration for private channels") return guild = message.guild config_json = json.dumps(config.get_guild(guild).get_dict(), indent=2, sort_keys=True) await message.channel.send(f"```json\n{config_json}\n```")
async def say(message, args): channel = args.split(" ")[0] output_message = args[len(channel) + 1:] try: await client.get_channel(util.safe_int(channel, None)).send(output_message) except discord.Forbidden: await message.channel.send("I don't have permission to send messages in that channel. Sorry!") except AttributeError: await message.channel.send("I couldn't find that channel. Sorry!")
def get_question(idx: str) -> dict: if idx is None: return {"error": "No ID"}, BAD_REQUEST idx = safe_int(idx) # pylint: disable=E1101 ques: Questions = get_ques_by_id(idx) if not ques: return no_question(idx) q = ques.as_json q.pop("special") return {**q, "game_over": False}
async def single_summon(message, args): """ Simulates a single summon on your current showcase. To choose a showcase to summon on, use the `showcase` command. """ total_summons = util.safe_int(args, 1) if total_summons < 1: await message.channel.send("I don't know how to do that many!") elif total_summons > 10: await message.channel.send("You can't do more than ten singles at a time!") else: results, text = db.perform_single_summons(message.channel.id, message.author.id, total_summons) with image.get_image_fp(results) as fp: await message.channel.send(text, file=discord.File(fp, filename="result.png"))
def get(s: str): gift_id = util.safe_int(s, None) return None if gift_id not in range(1, 8) else DragonGift(gift_id)
def get(s: str): type_id = util.safe_int(s, None) return None if type_id not in range(1, 9) else WeaponType(type_id)
def get(s: str): element_id = util.safe_int(s, None) return None if element_id not in range(1, 6) else Element(element_id)
def set_last_question_answered_at(user: UserTable, tstamp): ts = safe_int(tstamp) user.last_question_answered_at = ts save_to_db() return SUCCESS
def set_level(user: UserTable, level_to_set): level_to_set = safe_int(level_to_set) user.current_level = level_to_set save_to_db() return SUCCESS
def int0(s: str): return util.safe_int(s, 0)
def int(s: str): return util.safe_int(s, None)
async def send_typing(message, args): channel = client.get_channel(util.safe_int(args.strip(), 0)) await channel.trigger_typing()
def main(): stopwords = set(get_file_lines(STOPWORDS_FILE)) args = read_args() if args.queries_filename: queries = list( map( lambda q: (q[0], parse_query_str(q[1], stopwords, splitphrase=args.use_tfidf)), read_query_file(args.queries_filename))) else: queries = [("1", parse_query_str(args.query_str, stopwords, splitphrase=args.use_tfidf))] filename = args.collection_filename if not os.path.isfile(filename): eprint("Filename '%s' does not exist" % filename) return index_pickled_filename = filename + ".index" if args.refresh or not os.path.isfile(index_pickled_filename): eprint("Building a fresh index...") docmap = get_file_docmap(filename) for doc in docmap.values(): # Combine headline and text text = doc.headline + " " + doc.text # tokenize toks = tokenize(text, stopwords) # save it doc.tokens = list(toks) index = build_index(docmap) with open(index_pickled_filename, "wb") as f: eprint("Index built, saving to", index_pickled_filename, end="...") pickle.dump((docmap, index), f) eprint("done!") with open(index_pickled_filename + ".txt", "w") as f: f.write(index_to_str(index) + "\n") eprint(index_pickled_filename + ".txt", "updated!") else: eprint("Reading index from", index_pickled_filename) eprint() with open(index_pickled_filename, "rb") as f: docmap, index = pickle.load(f) if args.print_doc is not None: doc = docmap[args.print_doc] print(doc) print(doc.tokens) return if args.decimal_places is None: if args.use_tfidf: args.decimal_places = -1 else: args.decimal_places = 0 if args.decimal_places == -1: output_format = "{} 0 {} 0 {} 0" else: output_format = "{} 0 {} 0 {:." + str(args.decimal_places) + "f} 0" for pair in queries: key, q = pair results = search(docmap, index, q, args.use_tfidf) results = sorted( # sort by doc num ASC sorted(results, key=lambda d: safe_int(d.doc)), # and then by score DESC key=SearchResult.rank.fget, reverse=True) if args.debug: eprint(len(results), "documents, query: ", end="") eprint(pformat(pair)) if args.limit >= 0 and len(results) > args.limit: results = list(results)[:args.limit] for result in results: print(output_format.format(key, result.doc, result.rank)) if args.debug: eprint() eprint()