def addadmin(user, channel, admin): """Add a user to the list of admins""" if admin in config.get("admins"): msg(channel, "%s already is an admin" % admin) else: config.set("admins", "%s,%s" % (config.get("admins"), admin)) msg(channel, "%s has been added to the list of admins" % admin)
def addadmin(user, channel, text): """Add a user to the list of admins""" admin = text.split()[1] if is_admin(user): if admin in config.get("admins"): msg(channel, "%s already is an admin" % admin) else: config.set("admins", "%s,%s" % (config.get("admins"), admin)) msg(channel, "%s has been added to the list of admins" % admin)
def deladmin(user, channel, admin): """Remove a user from the list of admins""" if admin in config.get("admins"): admins = config.get("admins").split(",") admins.remove(admin) config.set("admins", ",".join(admins)) msg(channel, "%s has been removed from the list of admins" % admin) else: msg(channel, "Sorry, %s is not even an admin" % admin)
def init(): global database_path global db_connection database_path = get("database_path") db_connection = adbapi.ConnectionPool("sqlite3", database_path, check_same_thread=False, cp_openfun=_openfun, cp_min=1) def f(txn, *args): txn.execute("""CREATE TABLE IF NOT EXISTS author( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE);""") txn.execute("""CREATE TABLE IF NOT EXISTS quote( id INTEGER PRIMARY KEY AUTOINCREMENT, quote TEXT, author INTEGER NOT NULL REFERENCES author(id));""") txn.execute("""CREATE TABLE IF NOT EXISTS voter ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE);""") txn.execute("""CREATE TABLE IF NOT EXISTS vote ( id INTEGER PRIMARY KEY AUTOINCREMENT, vote INT NOT NULL, quote INTEGER NOT NULL REFERENCES quote(id), voter INTEGER NOT NULL REFERENCES voter(id), CONSTRAINT valid_vote CHECK (vote IN (-1, 1)), CONSTRAINT unique_quote_voter UNIQUE (quote, voter));""") return run_interaction(f)
def callback(quotes): max_quotes = int(get("max_quotes", 5)) if len(quotes) > max_quotes: msg(channel, "Too many results, please refine your search") else: messages = ["[%s] %s" % (id, quote) for (id, quote) in quotes] msg(channel, messages)
def asearch(user, channel, text): """Search for an anime""" try: name = " ".join(text.split()[1:]) except KeyError: pass logging.debug(name) results = anidb.search(name) max_results = int(get("max_search_results", 5)) if len(results) > max_results: msg(channel, "%s: Too many results, please refine your search" % user) return result_strings = [] for anime in results: titles = [] for lang in ("ja", "x-jat", "en", "de"): try: titles += [title.title for title in anime.titles[lang] if title.type == "main" or title.type == "official"] except KeyError: # There are no titles for that language pass result_strings.append("%i: %s" % (anime.id, ", ".join(titles))) msg(channel, result_strings)
def atags(user, channel, text): """Show the tags of an anime. Parameters: an aid""" anime = get_anime(user, channel, text) if anime is None: return anime.tags.sort(cmp=lambda x, y: cmp(int(x.count), int(y.count))) anime.tags.reverse() tags = [tag.name for tag in anime.tags] msg(channel, "Anime %s is tagged %s" % (anime.id, ", ".join(tags[:int(get("max_tags", 5))])))
def _topflopimpl(channel, text, top=True): """Shows quotes with the best or worst rating. If ``top`` is True, the quotes with the best ratings will be shown, otherwise the ones with the worst. """ if text: limit = int(text) else: limit = get("max_quotes") results = yield run_query( """ SELECT quote.id, quote.quote, sum(vote) as rating, count(vote) as votes FROM vote JOIN quote ON vote.quote = quote.id GROUP BY vote.quote ORDER BY rating %s LIMIT (?);""" % ("DESC" if top else "ASC"), [limit]) for row in results: msg(channel, MESSAGE_TEMPLATE_WITH_RATING % row)
def test_default(self): self.assertEqual("default", config.get("testkey", "default"))
def test_exists(self): config.set("key", "value") self.assertEqual("value", config.get("key"))
def test_set_default_options(self): self._set_default_options(stringkey="foo", defaultintkey="1") self.assertEqual(config.get("stringkey"), "foo") self.assertEqual(config.get_int("defaultintkey"), 1)
def admins(user, channel, text): """Show the list of admins""" msg(channel, config.get("admins"))