コード例 #1
0
ファイル: haibot.py プロジェクト: haitike/haibot
    def command_quote(self, bot, update, args):
        chat = update.message.chat_id
        sender = update.message.from_user
        no_write = _("You have no writting rights. See /list writers")

        help_text = _(
            "You can add a quote, selecting a message and clicking on \"reply\" and then writting /quote\n\n"
            "/quote search - show all quote matching a text (s/se)\n"
            "/quote <number> show the quote associated to a index\n"
            "/quote delete - delete a quote (d)\n"
            "/quote random - pick a random quote and show it (r/ra)\n"
            "/quote islist - Show the quotes as a list in the command /list (i/is)")
        if len(args) < 1:
            if update.message.reply_to_message:
                username = update.message.reply_to_message.from_user.name
                new_quote = update.message.reply_to_message.text
                new_index = lists.add_entry(new_quote, "quote", username)
                if not new_index:
                    self.send_message(bot, chat, _("Error: There is not Quote List in database. Use: /list lists add quote"))
                    haibot.logger.warning("There is not Quote List in database.")
                else:
                    self.send_message(bot, chat, _("Quote #%d was recorded") % (new_index))
            else:
                self.send_message(bot, chat, help_text)
        else:
            if args[0] == "delete" or args[0] == "d":
                if len(args) <2:
                    self.send_message(bot, chat, _("/quote delete <entry index>"))
                else:
                    if profile.get_user_value(sender.id, "is_writer"):
                        try:
                            if lists.has_entry_index(int(args[1]), "quote"):
                                deleted_entry = lists.delete_entry(int(args[1]), "quote")
                                self.send_message(bot, chat, _("Quote #%d was deleted.")%(deleted_entry["index"]))
                            else:
                                self.send_message(bot, chat, _("The quote number does not exist. Use /quote search <word>"))
                        except:
                            self.send_message(bot, chat, _("Use /quote delete <entry number>"))
                    else:
                        self.send_message(bot, chat, no_write)

            elif args[0] == "random" or args[0] == "r" or args[0] == "ra" :
                entry = lists.get_random_entry("quote")
                if entry:
                    self.send_message(bot, chat, "*%s*\n`[%d] %s`\n"
                                          % (entry["owner"], entry["index"], entry["entry"]), with_markdown=True)
                else:
                    self.send_message(bot, chat, _("There is no quotes"))

            elif args[0] == "search" or args[0] == "s" or args[0] == "se":
                if len(args) <2:
                    self.send_message(bot, chat, _("/quote search <words>"))
                else:
                    result = lists.search_entries(" ".join(args[1:]), "quote")
                    if result:
                        self.send_message(bot, chat, result)
                    else:
                        self.send_message(bot, chat, _("No quotes were found"))
            elif args[0] == "islist" or args[0] == "i" or args[0] == "is":
                if profile.get_user_value(sender.id, "is_writer"):
                    hidden = lists.toogle_hidden_list("quote")
                    if not hidden:
                        self.send_message(bot, chat, _("The quotes are now a list.\nBe careful: all the quotes can now be deleted, cloned or full showed"))
                    else:
                        self.send_message(bot, chat, _("The quotes are now hidden in list command."))
                else:
                    self.send_message(bot, chat, no_write)
            else:
                try:
                    if lists.has_entry_index(int(args[0]), "quote" ) :
                        entry = lists.get_entry( int(args[0]), "quote")
                        self.send_message(bot, chat, "*%s*\n`[%d] %s`\n"
                                          % (entry["owner"], entry["index"], entry["entry"]), with_markdown=True)
                    else:
                        self.send_message(bot, chat, _("Invalid number. Use /quote search <word>"))
                except:
                    self.send_message(bot, chat, help_text)
コード例 #2
0
ファイル: haibot.py プロジェクト: haitike/haibot
    def command_list(self, bot, update, args):
        chat = update.message.chat_id
        sender = update.message.from_user
        current_list = profile.get_user_value(sender.id,"current_list")
        sender_is_writer = profile.get_user_value(sender.id, "is_writer")
        no_writer_text = _("You have no writting rights")
        no_reader_text = _("You have no reading rights")
        non_existent_list_text = _("List \"%s\" does not exist. Select another list with /list use") % (current_list)

        if not profile.get_user_value(sender.id,"is_reader"):
            self.send_message(bot, chat, no_reader_text)
        else:
            help_text = _(
                """Use one of the following commands:
                /list show <all:done:notdone> - show entries in the current list (s)
                /list <number> - Show only one entry using his index
                /list add - add a new entry to the current list (a)
                /list delete - delete an entry from the current list (d)
                /list lists <show:add:delete:clone> - manage lists (l)
                /list use - select a list (makes the list the current list) (u)
                /list writers <show:add:delete> - manage admins for the list (w)
                /list readers <show:add:delete> - manage readers for the list (if apply) (r)
                /list done - mark an entry as <done> (do)
                /list random - pick a random entry and show it (ra)
                /list search - show all entries matching a text (se)""")
            if len(args) < 1:
                self.send_message(bot, chat, help_text)
            else:
                if args[0] == "show" or args[0] == "s":
                    show_help = False
                    if len(args) <2:
                        entry_list = lists.get_entries(current_list,mode="notdone")
                    else:
                        if args[1] == "done" or args[1] == "d":
                            entry_list = lists.get_entries(current_list, mode="done")
                        elif args[1] == "notdone" or args[1] == "n":
                            entry_list = lists.get_entries(current_list, mode="notdone")
                        elif args[1] == "all" or args[1] == "a":
                            entry_list = lists.get_entries(current_list, mode="all")
                        else:
                            show_help = True

                    if show_help:
                        self.send_message(bot, chat, _("Use /list show <all:done:notdone>"))
                    else:
                        if lists.has_list(current_list):
                            if entry_list:
                                entry_text= "%s: \n" % (current_list)
                                for entry in entry_list:
                                    if entry["done"]:
                                        entry_text += _("[%d][done] %s\n") % (entry["index"], entry["entry"] )
                                    else:
                                        entry_text += "[%d] %s\n" % (entry["index"], entry["entry"] )
                                self.send_message(bot, chat, entry_text)
                            else:
                                self.send_message(bot, chat, _("Your list is empty"))
                        else:
                            self.send_message(bot, chat, non_existent_list_text )

                elif args[0] == "add" or args[0] == "a":
                    if len(args) <2:
                        self.send_message(bot, chat, _("/list add <name>"))
                    else:
                        if sender_is_writer:
                            new_entry = " ".join(args[1:])
                            if lists.has_list(current_list):
                                new_index = lists.add_entry(new_entry,current_list, sender.name)
                                self.send_message(bot, chat, _("Entry #%d was added to list \"%s\"") % (new_index, current_list))
                            else:
                                self.send_message(bot, chat, non_existent_list_text )
                        else:
                            self.send_message(bot, chat, no_writer_text)

                elif args[0] == "delete" or args[0] == "d":
                    if len(args) <2:
                        self.send_message(bot, chat, _("/list delete <entry index>"))
                    else:
                        if sender_is_writer:
                            if lists.has_list(current_list):
                                try:
                                    if lists.has_entry_index(int(args[1]), current_list):
                                        deleted_entry = lists.delete_entry(int(args[1]),current_list)
                                        self.send_message(bot, chat, _("Entry #%d was deleted from list \"%s\".")%(deleted_entry["index"], current_list))
                                    else:
                                        self.send_message(bot, chat, _("The entry number does not exist. Use /list show"))
                                except:
                                    self.send_message(bot, chat, _("Use /list delete <entry number>"))
                            else:
                                self.send_message(bot, chat, non_existent_list_text )
                        else:
                            self.send_message(bot, chat, no_writer_text)

                elif args[0] == "lists" or args[0] == "l":
                    if len(args) <2:
                        self.send_message(bot, chat, "/list lists <show:add:delete:clone>")
                    else:
                        if args[1] == "show" or args[1] == "s":
                            show_text = ""
                            for list in lists.get_lists(enumerated=True):
                                show_text += "%s: %s\n" % (str(list[0]), list[1])
                            if show_text:
                                self.send_message(bot, chat, show_text)
                            else:
                                self.send_message(bot, chat, _("There is no lists. Create one."))
                        elif args[1] == "add" or args[1] == "a":
                            if len(args) <3:
                                self.send_message(bot, chat, _("/list lists add <something>"))
                            else:
                                if sender_is_writer:
                                    new_list = " ".join(args[2:])
                                    if lists.has_list(new_list):
                                        self.send_message(bot, chat, _("\"%s\" already exists!") % (new_list))
                                    else:
                                        list_index = lists.add_list(new_list, sender.name)
                                        self.send_message(bot, chat, _("\"%s\" list was created. Switch with /list use %d") % (new_list, list_index))
                                else:
                                    self.send_message(bot, chat, no_writer_text)

                        elif args[1] == "delete" or args[1] == "d":
                            if len(args) <3:
                                self.send_message(bot, chat, _("/list lists delete <list index>"))
                            else:
                                if sender_is_writer:
                                    list_array =  lists.get_lists(enumerated=True)
                                    was_deleted = False
                                    for list in list_array:
                                        try:
                                            if list[0] == int(args[2]):
                                                lists.delete_list(list[1])
                                                was_deleted = True
                                                self.send_message(bot, chat,
                                                    _("\"%s\" list was deleted. Use \"show\" for the new order. ")%(list[1]))
                                        except:
                                            was_deleted = False
                                    if not was_deleted:
                                        self.send_message(bot, chat, _("The list could not be deleted. Use:\n"
                                                                                         "/list lists delete <list index>"))
                                else:
                                    self.send_message(bot, chat, no_writer_text)

                        elif args[1] == "clone" or args[1] == "c":
                            if len(args) <4:
                                self.send_message(bot, chat, _("Use: /list clone <list_index> new name"))
                            else:
                                if sender_is_writer:
                                    list_array =  lists.get_lists(enumerated=True)
                                    was_cloned = False
                                    new_list = " ".join(args[3:])
                                    if lists.has_list(new_list):
                                        self.send_message(bot, chat, _("Cannot be cloned. \"%s\" already exists!") % (new_list))
                                    else:
                                        for list in list_array:
                                            try:
                                                if list[0] == int(args[2]):
                                                    was_cloned = lists.clone_list(list[1], new_list, sender.name)
                                                    if was_cloned:
                                                        self.send_message(bot, chat, _("\"%s\" list was cloned to \"%s\" and"
                                                                                " entries were ordered.")%(list[1], new_list))
                                            except:
                                               was_cloned = False
                                        if not was_cloned:
                                            self.send_message(bot, chat, _("The list could not be cloned. Use:\n"
                                                                                "Use: /list clone <list_index> new name"))
                                else:
                                    self.send_message(bot, chat, no_writer_text)

                        else:
                            self.send_message(bot, chat, "/list lists <show:add:delete:clone>")


                elif args[0] == "use" or args[0] == "u":
                    if len(args) <2:
                        self.send_message(bot, chat, _("/list use <list ID>\nUse /list lists show for IDs"))
                    else:
                        enumerated_list = lists.get_lists(enumerated=True)
                        is_changed = False
                        for list in enumerated_list:
                            try:
                                if list[0] == int(args[1]):
                                    profile.set_user_value(sender.id, "current_list", list[1])
                                    self.send_message(bot, chat, _("%s selected list: \"%s\"") % (sender.name, list[1]) )
                                    is_changed = True
                            except:
                                is_changed = False
                        if is_changed == False:
                            self.send_message(bot, chat, _("/Invalid ID. Use /list lists show") )

                elif args[0] == "writers" or args[0] == "w":
                    if len(args) <2:
                        self.send_message(bot, chat, "/list writers <show:add:delete:clone>")
                    else:
                        if args[1] == "show" or args[1] == "s":
                            show_text = ""
                            for list in profile.get_users("is_writer",with_name=True):
                                show_text += "%s: %s\n" % (list["user_id"], list["user_name"])
                            if show_text:
                                self.send_message(bot, chat, show_text)
                            else:
                                self.send_message(bot, chat, _("There is no writers."))
                                haibot.logger.warning("No writers. There should be one. Add bot owner to config file")

                        elif args[1] == "add" or args[1] == "a":
                            add_writers_help = _("/list writer add ID.\n Use \"/profile list\" for a list of known IDs")
                            if len(args) <3:
                                self.send_message(bot, chat, add_writers_help)
                            else:
                                if sender_is_writer:
                                    try:
                                        new_writer = int(args[2])
                                        if profile.has_user(new_writer):
                                            profile.set_user_value(new_writer, "is_writer", True)
                                            self.send_message(bot, chat, _("writer rights for (ID:%d) were added") % (new_writer))
                                        else:
                                            self.send_message(bot, chat, add_writers_help)
                                    except:
                                        self.send_message(bot, chat, add_writers_help)
                                else:
                                    self.send_message(bot, chat, no_writer_text)

                        elif args[1] == "delete" or args[1] == "d":
                            delete_writers_help = _("/list writer delete ID.\n Use \"/list writers show\" for a list of writers IDs")
                            if len(args) <3:
                                self.send_message(bot, chat, delete_writers_help)
                            else:
                                if sender_is_writer:
                                    try:
                                        del_writer = int(args[2])
                                        if profile.has_user(del_writer):
                                            if del_writer != sender.id:
                                                profile.set_user_value(del_writer, "is_writer", False)
                                                self.send_message(bot, chat, _("writer rights for (ID:%d) were deleted") % (del_writer))
                                            else:
                                                self.send_message(bot, chat, _("You can't delete your own rights"))
                                        else:
                                            self.send_message(bot, chat, delete_writers_help)
                                    except:
                                        self.send_message(bot, chat, delete_writers_help)
                                else:
                                    self.send_message(bot, chat, no_writer_text)
                        else:
                            self.send_message(bot, chat, "/list writers <show:add:delete>")

                elif args[0] == "readers" or args[0] == "r":
                    if len(args) <2:
                        self.send_message(bot, chat, "/list readers <show:add:delete:clone>")
                    else:
                        if args[1] == "show" or args[1] == "s":
                            show_text = ""
                            for list in profile.get_users("is_reader",with_name=True):
                                show_text += "%s: %s\n" % (list["user_id"], list["user_name"])
                            if show_text:
                                self.send_message(bot, chat, show_text)
                            else:
                                self.send_message(bot, chat, _("There is no readers."))
                                haibot.logger.warning("No readers. There should be one. Add bot owner to config file")

                        elif args[1] == "add" or args[1] == "a":
                            add_readers_help = _("/list reader add ID.\n Use \"/profile list\" for a list of known IDs")
                            if len(args) <3:
                                self.send_message(bot, chat, add_readers_help)
                            else:
                                if sender_is_writer:
                                    try:
                                        new_reader = int(args[2])
                                        if profile.has_user(new_reader):
                                            profile.set_user_value(new_reader, "is_reader", True)
                                            self.send_message(bot, chat, _("reader rights for (ID:%d) were added") % (new_reader))
                                        else:
                                            self.send_message(bot, chat, add_readers_help)
                                    except:
                                        self.send_message(bot, chat, add_readers_help)
                                else:
                                    self.send_message(bot, chat, no_writer_text)

                        elif args[1] == "delete" or args[1] == "d":
                            delete_readers_help = _("/list reader delete ID.\n Use \"/list readers show\" for a list of readers IDs")
                            if len(args) <3:
                                self.send_message(bot, chat, delete_readers_help)
                            else:
                                if sender_is_writer:
                                    try:
                                        del_reader = int(args[2])
                                        if profile.has_user(del_reader):
                                            if del_reader != sender.id:
                                                profile.set_user_value(del_reader, "is_reader", False)
                                                self.send_message(bot, chat, _("reader rights for (ID:%d) were deleted") % (del_reader))
                                            else:
                                                self.send_message(bot, chat, _("You can't delete your own rights"))
                                        else:
                                            self.send_message(bot, chat, delete_readers_help)
                                    except:
                                        self.send_message(bot, chat, delete_readers_help)
                                else:
                                    self.send_message(bot, chat, no_writer_text)
                        else:
                            self.send_message(bot, chat, "/list readers <show:add:delete>")

                elif args[0] == "done" or args[0] == "do":
                    if len(args) <2:
                        self.send_message(bot, chat, _("/list done <entry index>"))
                    else:
                        if sender_is_writer:
                            if lists.has_list(current_list):
                                try:
                                    if lists.has_entry_index(int(args[1]), current_list):
                                        entry, done_status = lists.toogle_done_entry(int(args[1]),current_list)
                                        if done_status:
                                            self.send_message(bot, chat,_("#%d: done (list:%s)")%(entry["index"],current_list))
                                            self.send_message(bot, chat,_("See: \list show <all:done:notdone>"))
                                        else:
                                            self.send_message(bot, chat,_("#%d: notdone (list:%s)")%(entry["index"],current_list))
                                    else:
                                        self.send_message(bot, chat, _("The entry number does not exist. Use /list show all"))
                                except:
                                    self.send_message(bot, chat, _("Use /list done <entry number>"))
                            else:
                                self.send_message(bot, chat, non_existent_list_text )
                        else:
                            self.send_message(bot, chat, no_writer_text)

                elif args[0] == "random" or args[0] == "ra":
                    if lists.has_list(current_list):
                        entry = lists.get_random_entry(current_list)
                        if entry:
                            if entry["done"] == True:
                                self.send_message(bot, chat, _("[%d][done] %s\n") % (entry["index"], entry["entry"]))
                            else:
                                self.send_message(bot, chat, "[%d] %s\n" % (entry["index"], entry["entry"]))
                        else:
                            self.send_message(bot, chat, _("Your list is empty"))
                    else:
                        self.send_message(bot, chat, non_existent_list_text )

                elif args[0] == "search" or args[0] == "se":
                    if len(args) <2:
                        self.send_message(bot, chat, _("/list search <words>"))
                    else:
                        if lists.has_list(current_list):
                            result = lists.search_entries(" ".join(args[1:]), current_list)
                            if result:
                                self.send_message(bot, chat, result)
                            else:
                                self.send_message(bot, chat, _("No entries were found"))
                        else:
                            self.send_message(bot, chat, non_existent_list_text )

                else:
                    try:
                        if lists.has_entry_index(int(args[0]), current_list ) :
                            if lists.has_list(current_list):
                                entry = lists.get_entry( int(args[0]), current_list)
                                if entry["done"] == True:
                                    self.send_message(bot, chat, _("[%d][done] %s\n") % (entry["index"], entry["entry"]))
                                else:
                                    self.send_message(bot, chat, "[%d] %s\n" % (entry["index"], entry["entry"]))
                            else:
                                self.send_message(bot, chat, non_existent_list_text )
                        else:
                            self.send_message(bot, chat, _("Invalid number. Use /list show o /list search <word>"))
                    except:
                        self.send_message(bot, chat, help_text)