def sendBuyReputation(self, chatid, bot, edit=False): # variable definition sdb = {} sdb['rep_points'] = self.getRepPointsStr() sdb['karma'] = self.getKarmaStr() sdb['rep'] = self.getReputationStr() sdb['points'] = self.getPointsStr() # message pattern definition msg = "<b>--- Buy reputation ---</b>\n" msg += "<i>Higher reputation ensures your media will be shown first, namely more reputation, more points</i>\n" msg += "\n" msg += "You currently have {rep_points}\n" msg += "You a karma of {karma}\n" msg += "This generates a reputation of {rep}\n" msg += "Reputation formula:\n <code>karma * reputation points</code>\n" msg += "\n" msg += "You have {points} points\n" msg += "Vould you like to buy more RP?\n" # message translation msg = _(msg, self.lang_tag) msg = msg.format(**sdb) blist = [] qrp = 1 for i in range(3): total_cost = calc_rep_cost(self, qrp) #variable def sdb = {} sdb["qrp"] = str(em.RPstr(qrp)) sdb["points"] = str(em.Pstr(total_cost)) buyrptext = _("Buy {qrp} for {points}", self.lang_tag) buyrptext = buyrptext.format(**sdb) button = [InlineKeyboardButton(text= buyrptext, callback_data = "buy_rp_" + str(qrp))] blist.append(button) qrp = (i+1)*2 # button = [InlineKeyboardButton(text=_("Calculate media probability (not working)", self.lang_tag), # callback_data = "buy_calcp"),] # blist.append(button) rmk = InlineKeyboardMarkup(inline_keyboard = blist) if edit: assert type(chatid) == tuple and len(chatid) == 2 bot.editMessageText(chatid, msg, parse_mode="HTML", reply_markup = rmk) else: bot.sendMessage(chatid, msg, parse_mode="HTML", reply_markup = rmk)
def sendMainMenu(self, chatid, user): ''' The function shows the user the main menu of the bot''' m = "<b> prot --- Main Menu --- prot </b>\n" m += "<i>Choose a category to see or vote the pictures, media, belonging to it</i>\n" m += "\n" m += "<b>--- Category ---</b>\n" m += "START HERE!!\n" m += "/categories\n" m += "\n" m += "<b>--- Top users and media ---</b>\n" m += "<i>The top chart of media and users by category</i>\n" m += "/top_media\n" m += "\n" m += "<b>--- Profile ---</b>\n" m += "<i>Your profile containing the information about points </i>\n" m += "/profile\n" m += "\n" m += "<b>--- Upload ---</b>\n" m += "<i>Upload your media to the bot</i>\n" m += "/upload\n" m += "\n" m += "<b>--- HELP ---</b>\n" m += "/help\n" m += "\n" m = _(m, user.lang_tag) self.bot.sendMessage(chatid, m, parse_mode="HTML")
def help_msg(version, lang_tag): # variables sdb = {} sdb["version"] = version sdb["pem"] = em.points_emoji sdb["kem"] = em.karma_emoji sdb["rem"] = em.reputation_emoji help_msg = "@DirtyRateBot version: {version}\n" help_msg += "<i>This bot allows you to upload and rate media with the rest of the bot community.\nThe media are divided into categories.</i>\n" help_msg += "\n" help_msg += "<b>- Rating pictures -</b>\n" help_msg += "To rate the media use /categories\n" help_msg += "To see the top three media use the /show_top\n" help_msg += "\n" help_msg += "<b>- Uploading pictures -</b>\n" help_msg += "To upload your media see /help_upload.\n" help_msg += "\n" help_msg += "<b>- Profile -</b>\n" help_msg += "You can check your uploads under /profile then /my_uploads.\n" help_msg += "You will have a profile with points {pem}, karma {kem} and reputation {rem}.\nSee /help_points, /help_karma, /help_reputation\n" help_msg += "\n" help_msg += "<b>- Super Groups -</b>\n" help_msg += "You can use the bot in super group, add it and press the command /vote.\n" help_msg += "If you are the admin you can set the categories you want to show /help_supergroups." help_msg = _(help_msg, lang_tag) help_msg = help_msg.format(**sdb) return help_msg
def addCategory(self, chatid, user): ''' This function is called when a user wants to create a category chatid must be private and is checked in the handle function user is te requesting user ''' # first send the available categories self.sendSelectCategoryMenu(chatid, sort=True) # create the message to purchase a category sdb = {} sdb["price"] = em.Pstr(self.categoryPrice(user), True) sdb["points"] = user.getPointsStr(True) m = "Send a category name\n" m += "<i>The name must be maximum 15 characters long and can contain only alphanumeric characters (a-z and A-Z and 1-10)</i>\n" m += "\n" m += "Create a category will cost {price} you have {points}\n" m += "/cancel\n" m = _(m, user.lang_tag) m = m.format(**sdb) self.bot.sendMessage(chatid, m, parse_mode="HTML") self.new_cat_req[chatid] = (True, False)
def help_msg_upload(lang_tag): help_msg = "<b>- Upload media -\n</b>" help_msg += "Use the command /upload to upload media to the bot.\n" help_msg += "The bot will ask for a media (can be a picture, gif, video or document)\n" help_msg += "The bot will ask for a category\n" help_msg += "If the bot says success, you made it" help_msg = _(help_msg, lang_tag) return help_msg
def voteCategoryPrivate(self, chatid, user, categoryname, chatsdb): medialist = [] for dmedia in self.media_vote_db.values(): media = dmedia.getData() if media.catname == categoryname and media.id not in user.dont_show_pics_id and not media.deleted: medialist.append(media) nmediacat = len(medialist) print("category: ", categoryname, "has :", nmediacat) if nmediacat > 0: # count all the picture the user didnt vote and get the # reputation of each user that uploaded the media usermedia = [] for media in medialist: if user.id not in media.votersids: usermedia.append(media) nusermedia = len(usermedia) print("in this category user", user.id, "voted", nmediacat - nusermedia) if nusermedia > 0: chosen_media = self.pickMedia(usermedia) print("The user will vote: ", chosen_media) # propose picture chosen_media.showMediaVote(chatid, self, chatsdb) else: self.bot.sendMessage( chatid, _("You voted all the pictures in this category use", user.lang_tag)) self.sendMainMenu(chatid, user) print("All pictures have been voted") else: self.bot.sendMessage(chatid, _("No media in this category", user.lang_tag)) self.sendMainMenu(chatid, user) print("no media in this category")
def showPoints(self): sdb = {} sdb["points"] = self.mypoints msg = "My points = {points}" msg = _(msg, "it-IT") msg = msg.format(**sdb) return msg
def sendProfileInfo(self, chatid, bot, catManager): # variable definition sdb = {} sdb['nup'] = self.countUploadedContent(catManager) sdb['anonid'] = self.anonid sdb['dup'] = self.dayuploads sdb['points'] = self.getPointsStr(True) sdb['karma'] = self.getKarmaStr(True) sdb['reputation'] = self.getReputationStr(True) sdb['rep_points'] = self.getRepPointsStr(True) sdb['anon_emoji'] = em.anon_emoji sdb['user_face'] = em.user_face sdb['space_shuttle'] = em.space_shuttle # message pattern definition m = "<b>- {anon_emoji} User Profile {user_face} {space_shuttle}-</b>\n" m += "<i>Your anonymous id is: {anonid}</i>\n" m += "Change nickname: /set_nickname\n" m += "\n" m += "<b>- Current Status -</b>\n" m += "Points: {points} | /help_points\n" m += "Karma: {karma} | /help_karma\n" m += "Reputation points: {rep_points}\n" m += "Reputation: {reputation}\n/help_reputation\n" m += "<i>To increase your reputation use</i>\n/buy_reputation\n" m += "\n" m += "<b>- My Uploads -</b>\n" m += "You uploaded a total of {nup} media\n" m += "Today you uploaded {dup} media\n" m += "To see your uploaded media press\n/my_uploads\n" m += "\n" m += "<b>- Create a category -</b>\n" m += "/add_category\n" m += "\n" m += "<b>--- Users top chart ---</b>\n" m += "/user_top\n" m += "\n" m += "<b>--- Users top chart ---</b>\n" m += "/set_language\n" m += "\n" m += "/main_menu" # message translation mym = _(m, self.lang_tag) # assing the patterns mym = mym.format(**sdb) bot.sendMessage(chatid, mym, parse_mode= "HTML")
def sendUploadCategory(self, msg): chatid = msg.chat.id user = self.user_profile_db.getData(msg.mfrom.id).getData() print("-sendUploadCategory-") print(self.getupload[chatid], self.getuploadMedia[chatid]) if self.getupload[chatid] and self.getuploadMedia[chatid] == "success": # show categories self.sendSelectCategoryMenu(chatid, sort=True) self.bot.sendMessage( chatid, _( "The media will be uploaded underthe category you chose:\nSend category name:", user.lang_tag)) # set categories to be accepted self.getuploadCategory[chatid] = True
def sendAdmin(self, chatid, bot, userdb, catManager = None, ban_keyboard = False): # send the message in format # reported media # user who sent the media # send media # variable definition sdb = {} sdb["user"] = str(self.getUser(userdb).anonid) sdb["category"] = self.catname sdb["tag"] = "not defined" if catManager is not None: sdb["tag"] = str(catManager.categories_db.getData(self.catname).getData().tag) sdb["score"] = self.getScoreF() # pattern definition cpt = "User {user} upoaded the picture\n" cpt += "Category: {category}\n" cpt += "Category tag: {tag}\n" cpt += "Media score: {score}\n" # get category admin langtag = "en-EN" if catManager is not None: creatorid = catManager.categories_db.getData(self.catname).getData().creator if creatorid is not None: creator = catManager.user_profile_db.getData(creatorid).getData() if creator is not None: langtag = creator.lang_tag cpt = _(cpt, langtag) cpt = cpt.format(**sdb) rmk = self.makeKeyboardAdminPhoto() self.sendMedia(chatid, cpt, rmk, bot) # send banning options if ban_keyboard: rmkban = self.getUser(userdb).makeKeyboardAdminBan() bot.sendMessage(chatid, "User: " + str(self.getUser(userdb)), reply_markup = rmkban )
def help_msg_points(lang_tag): # variables sdb = {} sdb["pemoji"] = em.points_emoji sdb["1 point"] = str(em.Pstr(1)) sdb["5 points"] = str(em.Pstr(5)) sdb["remoji"] = em.reputation_emoji help_msg = "<b>- Points {pemoji} -</b>\n" help_msg += "You earn +{1 point} when you vote and +{5 points} when you /upload.\n" help_msg += "When somebody upvotes your content you earn +{1 point} but you lose -{1 point} if somebody downvotes.\n" help_msg += "Points are used to increase your reputation {remoji} (/buy_reputation), which increases the visibility of your media.\n" help_msg = _(help_msg, lang_tag) help_msg = help_msg.format(**sdb) return help_msg
def help_msg_karma(lang_tag): sdb = {} sdb['kem'] = em.karma_emoji sdb['uem'] = em.upvote_emoji sdb['anonem'] = em.anon_emoji sdb['dem'] = em.downvote_emoji help_msg = "<b>- karma {kem} -</b>\n" help_msg += "The karma is calculated by the number of upvotes {uem} minus the number of downvotes {dem} and multiplied by the number of people {anonem} who voted for your media.\n" help_msg += "The karma influences the way the uploaded pictures are presented to the users.\nThe more karma you have the better visibility will get your pictures.\n" help_msg += "\n" help_msg += "- How do I earn karma?\n" help_msg += "Upload media to the bot by using the commnad /upload\n" help_msg = _(help_msg, lang_tag) help_msg = help_msg.format(**sdb) return help_msg
def help_msg_supergroups(lang_tag): help_msg = "<b>- Help supergroup -</b>\n" help_msg += "The users can call /vote and the bot will pick media from a set of selected categories.\n" help_msg += "If you are one of the admin or the creator of the supergroup, you can decide what categories will be shown.\n" help_msg += "By default the bot will show the safe for work (SFW) categories: meme, nature, space\n" help_msg += "Admin can use a series of commands to set new categories or remove them.\n" help_msg += "List of admin commands: (use them in group)\n" help_msg += "/set_all: sets all the categories\n" help_msg += "/rem_all: mutes all the categories\n" help_msg += "/set_categories: the admin can select a number of categories comma separated.\n" help_msg += "/rem_categories: select which categories to remove.\n" help_msg += "/reset_all: sets only the default categories.\n" help_msg += "/set_porngore: allows the p**n and gore categories to be shown.\n" help_msg += "/set_kinky: adds the p**n categories but not the gore categories, to the shown list.\n" help_msg += "/list_categories: shows the active categories in this group\n" help_msg = _(help_msg, lang_tag) return help_msg
def help_msg_reputation(lang_tag): sdb = {} sdb["remoji"] = em.reputation_emoji sdb["anonem"] = em.anon_emoji sdb["kemoji"] = em.karma_emoji help_msg = "<b>- Reputation {remoji} -</b>\n" help_msg += "The reputation is shown near your username (or anonymous id {anonem}).\n" help_msg += "The reputation is calculated by your reputation points (/buy_reputation) times your karma {kemoji} (if karma is positve).\n" help_msg += "\n" help_msg += "The reputation determines the media's visibility.\nWhen a person wants to vote, the media are orderd by reputation of the user.\n" help_msg += "\n" help_msg += "- How do I increase my reputation?\n" help_msg += "use the command /buy_reputation" help_msg = _(help_msg, lang_tag) help_msg = help_msg.format(**sdb) return help_msg
def showCategoryPrivate(self, chatid, user, categoryname, chatdb): # upon a request to show a media -> send a random media # construct a paging interface medialist = [] catnamelist = self.getCategoriesNamesList() if categoryname in catnamelist: for dmedia in self.media_vote_db.values(): media = dmedia.getData() if media.catname == categoryname and media.id not in user.dont_show_pics_id and not media.deleted: medialist.append(media) nmediacat = len(medialist) print("category:", categoryname, "has:", nmediacat) if nmediacat > 0: media = random.choice(medialist) media.showMediaShow(chatid, self, chatdb=chatdb) else: self.bot.sendMessage(chatid, _("No media in this category", user.lang_tag)) self.sendMainMenu(chatid, user) print("no media in this category")
def info(self): # variable definition sdb = {} sdb["uid"] = self.uid sdb["content"] = str(self.content.type) sdb["user"] = self.getUser().id sdb["karma"] = self.getKarma() sdb["reputation"] = self.getReputation() sdb["deleted"] = self.deleted sdb["reported by"] = len(self.reported_by) # pattern definition s = "-Media info-\n" s += "Media uploaded by {user}, {uid}, {content}\n" s += "Media has karma {karma} and reputation {reputation}\n" s += "Media is deleted {deleted} and got reported by {reported by}" # translation s = _(s) # pattern assignment s = s.format(**sdb) return s
def sendNotification(self, notification_tag, notification_message, bot, chatsdb): # notification tags: # category-vote # category-newmedia if notification_tag not in self.receive_notifications: self.receive_notifications[notification_tag] = True if self.receive_notifications[notification_tag] and self.isActive: button = InlineKeyboardButton( text= _('mute notification ', self.lang_tag) + em.report_emoji, callback_data='mute_' + notification_tag + '_' + str(self.id) ) keyboard = InlineKeyboardMarkup(inline_keyboard=[[button,],]) try: bot.sendMessage(self.getChatID(chatsdb), notification_message, parse_mode = "HTML", reply_markup= keyboard) except TelegramError as tge: print("Send notif:", tge.description) self.isActive = False except Exception as e: print(e) raise e
def getCategoryName(self, chatid, user, content): ''' this function gets the name from the chat once the user wants to add a category ''' # check if the user requested a "add category" if self.new_cat_req[chatid][0]: if (content is not None and content.type == "text" and not content.text.startswith("/")): # check validity of category name # a category must be max 15 character long categoryname = content.text is_valid_name = True if len(categoryname) > 15: is_valid_name = False validcharset = string.ascii_lowercase + string.ascii_uppercase + string.digits for c in categoryname: if c not in validcharset: is_valid_name = False break if (is_valid_name and categoryname.lower() in map( lambda s: s.lower(), list(self.categories_db.keys())) and categoryname.lower() != "top"): is_valid_name = False # if the name is valid # create a message where the buttons displays the possible tags if is_valid_name: # create message price = self.categoryPrice(user) sdb = {} sdb["catname"] = categoryname sdb["price"] = em.Pstr(price, long=True) m = "Select a tag for the category\n" m += "<i>Your category will be banned if you have for example p**n in a safe for work (SFW-tag category</i>\nThis action will cost you {price}\n\n" m += "category: {catname}\n\n/main_menu" m = _(m, user.lang_tag) m = m.format(**sdb) # create keyboard strtag = "nsfw" cbstr = 'createcat_' + categoryname + "_" + strtag + "_" + str( price) button_nsfw = InlineKeyboardButton(text=strtag, callback_data=cbstr) strtag = "sfw" cbstr = 'createcat_' + categoryname + "_" + strtag + "_" + str( price) button_sfw = InlineKeyboardButton(text=strtag, callback_data=cbstr) strtag = "gore" cbstr = 'createcat_' + categoryname + "_" + strtag + "_" + str( price) button_gore = InlineKeyboardButton(text=strtag, callback_data=cbstr) ld_row = [button_nsfw, button_sfw, button_gore] keyboard = InlineKeyboardMarkup(inline_keyboard=[ld_row]) self.bot.sendMessage(chatid, m, parse_mode="HTML", reply_markup=keyboard) self.new_cat_req[chatid] = (True, True) else: self.bot.sendMessage( chatid, _( "Name not valid (either character or category already present) /add_category again or /main_menu", user.lang_tag)) self.new_cat_req[chatid] = (False, False) else: self.bot.sendMessage( chatid, _( "Operation aborted (must be text and not a command) /add_category again or /main_menu", user.lang_tag)) self.new_cat_req[chatid] = (False, False)
def getUploadCategory(self, msg): ''' This function is the core function of the uploading process the function checks if the media can be created, the function is activated only if the user requested an upload (either by direct posting or command /upload) and the user provided a category name categoryname: must be the lowercase representation of one of the category if the user chosed a valid category the program will try to create a media ''' chatid = msg.chat.id if self.getuploadCategory[chatid] == True and self.getuploadMedia[ chatid] == "success": # get the category message categories duser = self.user_profile_db.getData(msg.mfrom.id) user = duser.getData() if msg.content.type == "text": usercatname = msg.content.text.lower() # check if category is a real category catnamelist = self.getCategoriesNamesList() if usercatname in catnamelist: # get the user user.tmp_content["cateogry"] = usercatname.lower() # preventively # change this might create error print("try to create media") # temporarily assign none to the short id mediavote = ContentVote(None, user.id, user.tmp_content["media"], user.tmp_content["cateogry"]) # if the picture is already present isoc = True for media in self.media_vote_db.values(): if media.getData( ).content.file_id == mediavote.content.file_id: isoc = False if isoc: dmediavote = Data(mediavote.id, mediavote) if self.media_vote_db.isNew(dmediavote): # assign here the uid self.uid += 1 dmediavote.getData().uid = self.uid # add the data to the database self.media_vote_db.addData(dmediavote) self.media_vote_db.updateDb() # mange the users data user.dayuploads += 1 user.points += 5 # update the user data duser.setData(user) self.user_profile_db.updateDb() # reset the upload media switches self.getuploadCategory[chatid] = False self.getuploadMedia[chatid] = False self.getupload[chatid] = False self.bot.sendMessage( chatid, _("Content uploaded successfully", user.lang_tag)) self.bot.sendMessage( chatid, "You earned " + str(em.Pstr(5)) + " /profile") print(dmediavote.getData(), "added successfully") return True else: self.bot.sendMessage( chatid, _("Media already in database", user.lang_tag)) else: self.bot.sendMessage( chatid, _( "Category not found\nYou can add the category using /add_category", user.lang_tag)) else: self.bot.sendMessage(chatid, _("Message must be text", user.lang_tag)) self.bot.sendMessage( chatid, "Category: Something went wrong use /upload again") self.getuploadCategory[chatid] = False self.getuploadMedia[chatid] = False self.getupload[chatid] = False
def checkNickname(self, chatid, user, nickname): # the nickname can be min 3 char long and max 15 char long # the nickname must be in the alphanumeric set # the lowercase representation of the nickname must not be repeated is_valid = False if type(nickname) is str: if len(nickname) > 3 and len(nickname) < 15: is_valid = True else: self.bot.sendMessage( chatid, _("Nickname must be over 3 char and below 15 char", user.lang_tag)) user.tmp_nickname = "" else: self.bot.sendMessage(chatid, _("Nickname must be string", user.lang_tag)) user.tmp_nickname = "" if is_valid: is_ascii = True for c in nickname: if c not in string.ascii_letters + string.digits: is_ascii = False # define variabbles sdb = {} sdb["character"] = c # define message m = "This [{character}] symbol cannot be used in name\n /main_menu" # translate m = _(m, user.lang_tag) # assign variables m = m.format(**sdb) self.bot.sendMessage(chatid, m) break if is_ascii: is_unique = True for dcuser in self.user_profile_db.values(): cuser = dcuser.getData() if cuser.anonid.lower() == nickname.lower(): is_unique = False self.bot.sendMessage( chatid, _("Nickname already in database\n /main_menu", user.lang_tag)) break if is_unique: user.anonid = nickname duser = Data(user.id, user) user.tmp_nickname = "" self.user_profile_db.setData(duser) self.user_profile_db.updateDb() self.bot.sendMessage( chatid, _("Nickname changed successfully\n /main_menu", user.lang_tag)) return True else: user.tmp_nickname = "" return False else: user.tmp_nickname = "" return False else: user.tmp_nickname = "" return False
def sendUserTopCategory(self, chatid, requser, catnames, ipage=None): userlist = self.generateUserList(sort=["reputation", "karma"], excluded_ids=[creator_id], catnames=catnames) if userlist: maxperpage = 10 ulistpage, page, maxpage = self.sliceListForPage( userlist, ipage, maxperpage) categories = [ cat for cat in self.categories_db.getDataGen() if cat.name in catnames ] sdb = {} sdb["ipos"] = (page - 1) * maxperpage + 1 sdb["endpos"] = sdb["ipos"] + maxperpage sdb["catnames"] = "|".join(cat.screen_name for cat in categories) s = '<b>--- For the categories: {catnames} ---</b>\n'.format(**sdb) s += 'Form {ipos} to {endpos} User Chart\n'.format(**sdb) s += "\n" position = (page - 1) * maxpage + 1 is_user_in_page = False fillcharposition = "0" for user in ulistpage: # prepare the variables to be printed such position and reputation sdb = {} sdb["position"] = position sdb["anonid"] = user.anonid sdb["reputation"] = user.getReputationStr() sdb["karma"] = user.getKarmaStr() # if the user is in this page it gets a (you) tag and is not displayed # at the chart bottom if user.id == requser.id: is_user_in_page = True sdb["you"] = "(you)" else: sdb["you"] = "" if position == (page - 1) * maxperpage + 1: fillcharposition = len(str(position + maxperpage)) sdb["fposition"] = "{position: <{0}}".format( fillcharposition, position=sdb["position"]) s += "<code>{fposition}.{anonid:_^15}{you}|{reputation}|{karma}</code>\n".format( **sdb) position += 1 if not is_user_in_page: # find the requser position position = 0 for user in userlist: position += 1 if user.id == requser.id: break sdb = {} sdb["position"] = position sdb["anonid"] = requser.anonid sdb["reputation"] = requser.getReputationStr() sdb["karma"] = requser.getKarmaStr() sdb["you"] = "(you)" s += "...\n{position}. {anonid}{you}|R{reputation}|K{karma}\n...\n".format( **sdb) s += "\n" tot_users = len(userlist) s += "<i>Users in this category: " + str(tot_users) + "</i>\n" self.sendPage(chatid, maxpage, maxperpage, s, page, ipage, "cmputc", args=catnames) else: self.bot.sendMessage( chatid, _("No media in this category", requser.lang_tag))