def run(self, m): text = None if is_command(self, 1, m.content) or is_command(self, 3, m.content): try: tag = subprocess.check_output( ['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').rstrip('\n') except: tag = 'latest' greeting = self.bot.trans.plugins.about.strings.greeting % self.bot.info.first_name version = self.bot.trans.plugins.about.strings.version % tag license = self.bot.trans.plugins.about.strings.license help = self.bot.trans.plugins.about.strings.help % self.bot.config.prefix about = self.bot.trans.plugins.about.strings.about % self.bot.config.prefix notice = self.bot.trans.plugins.about.strings.notice donations = self.bot.trans.plugins.about.strings.donations % self.bot.config.prefix stats = self.bot.trans.plugins.about.strings.stats % (len( self.bot.users), len(self.bot.groups)) if is_command(self, 1, m.content): text = '%s\n\n%s\n\n%s\n%s\n\n%s\n%s\n\n%s\n\n%s' % ( greeting, notice, help, about, version, donations, license, stats) else: text = '%s\n\n%s\n\n%s\n%s\n\n%s' % (greeting, notice, help, about, donations) elif is_command(self, 2, m.content): donations_explanation = self.bot.trans.plugins.about.strings.donations_explanation supporters_title = self.bot.trans.plugins.about.strings.supporters supporters = '' for uid in self.bot.users: if has_tag(self.bot, uid, 'supporter') or has_tag( self.bot, uid, 'supporter:?'): supporters += '\n • %s' % self.bot.users[uid].first_name if 'last_name' in self.bot.users[uid] and self.bot.users[ uid].last_name: supporters += ' %s' % self.bot.users[uid].last_name if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: supporters += ' @%s' % self.bot.users[uid].username for tag in has_tag(self.bot, uid, 'supporter:?', True): if ':' in tag: supporters += ' | %s' % tag.split(':')[1] if len(supporters) > 0: text = '%s\n\n%s%s' % (donations_explanation, supporters_title, supporters) else: text = donations_explanation self.bot.send_message(m, text, extra={ 'format': 'HTML', 'preview': False })
def get_conversations_to_post(self, topic='?'): conversations = [] if self.bot.users: for cid in self.bot.tags: if has_tag(self.bot, cid, 'autopost:' + topic) or has_tag( self.bot, cid, 'autopost'): conversations.append(cid) return conversations
def run(self, m): input = get_input(m) if not is_trusted(self.bot, m.sender.id): return self.bot.send_message( m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not input: return self.bot.send_message( m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if m.reply: target = str(m.reply.sender.id) name = m.reply.sender.first_name elif first_word(input) == '-g': target = str(m.conversation.id) name = m.conversation.title input = all_but_first_word(input) elif first_word(input).isdigit(): target = first_word(input) name = target input = all_but_first_word(input) else: target = str(m.sender.id) name = m.sender.first_name tags = input.split() # Adds a tag to user or group. # if is_command(self, 1, m.content): for tag in tags: if not has_tag(self.bot, target, tag): set_tag(self.bot, target, tag) return self.bot.send_message( m, self.bot.trans.plugins.tags.strings.tagged % (name, tags), extra={'format': 'HTML'}) # Removes a tag from user or group. # elif is_command(self, 2, m.content): for tag in tags: if has_tag(self.bot, target, tag): del_tag(self.bot, target, tag) return self.bot.send_message( m, self.bot.trans.plugins.tags.strings.untagged % (name, tags), extra={'format': 'HTML'})
def always(self, m): if str(m.conversation.id) == str( self.bot.config.alerts_conversation_id) or str( m.conversation.id) == str( self.bot.config.admin_conversation_id): return spam_types = ['spam', 'arab', 'russian'] for spam_type in spam_types: if has_tag(self.bot, m.sender.id, spam_type): if not is_admin(self.bot, m.sender.id, m): self.kick_spammer(m, spam_type, 'tag') elif is_trusted(self.bot, m.sender.id, m): del_tag(self.bot, m.sender.id, spam_type) name = get_full_name(self.bot, m.sender.id) gid = str(m.conversation.id) self.bot.send_admin_alert( 'Unmarking %s: %s [%s] from group %s [%s]' % (spam_type, name, m.sender.id, self.bot.groups[gid].title, gid)) if m.conversation.id < 0 and has_tag( self.bot, m.conversation.id, spam_type) and not has_tag( self.bot, m.conversation.id, 'safe') and not has_tag( self.bot, m.conversation.id, 'resend:?') and not has_tag( self.bot, m.conversation.id, 'fwd:?'): self.kick_myself(m) if m.extra: if 'urls' in m.extra: for url in m.extra['urls']: self.check_trusted_telegram_link(m, fix_telegram_link(url)) if 'caption' in m.extra and m.extra['caption']: if self.detect_arab(m.extra['caption']): self.kick_spammer(m, 'arab', 'caption') if self.detect_russian(m.extra['caption']): self.kick_spammer(m, 'russian', 'caption') try: self.check_trusted_telegram_link(m, m.extra['caption']) except: logging.error(m.extra['caption']) if not has_tag(self.bot, m.conversation.id, 'safe') and not has_tag( self.bot, m.conversation.id, 'resend:?') and not has_tag( self.bot, m.conversation.id, 'fwd:?'): if m.type == 'text': if self.detect_arab(m.content): self.kick_spammer(m, 'arab', 'content') if self.detect_russian(m.content): self.kick_spammer(m, 'russian', 'content') if self.detect_arab(m.sender.first_name): self.kick_spammer(m, 'arab', 'name') if self.detect_russian(m.sender.first_name): self.kick_spammer(m, 'russian', 'name')
def kick_spammer(self, m, spam_type='spam', content='content'): name = get_full_name(self.bot, m.sender.id) gid = str(m.conversation.id) if content == 'name': text = m.sender.first_name elif content == 'title': text = m.conversation.title else: text = m.content if not has_tag(self.bot, m.sender.id, spam_type): self.bot.send_admin_alert( 'Marked as {}: {} [{}] from group {} [{}] for {}: {}'.format( spam_type, name, m.sender.id, self.bot.groups[gid].title, gid, content, text)) set_tag(self.bot, m.sender.id, spam_type) if spam_type in self.bot.groups[gid]: self.bot.groups[gid][spam_type] += 1 else: self.bot.groups[gid][spam_type] = 1 set_data('groups/%s/%s' % (self.bot.name, gid), self.bot.groups[gid]) if self.bot.groups[gid][spam_type] >= 10 or str( m.sender.id) == str(m.conversation.id): set_tag(self.bot, gid, spam_type) self.bot.send_admin_alert( 'Marked group as %s: %s [%s]' % (spam_type, self.bot.groups[gid].title, gid)) if not has_tag(self.bot, m.conversation.id, 'safe') and not has_tag( self.bot, gid, 'resend:?') and not has_tag( self.bot, gid, 'fwd:?'): self.kick_myself(m) if im_group_admin(self.bot, m) and has_tag(self.bot, m.conversation.id, 'anti' + spam_type): self.bot.bindings.kick_conversation_member(m.conversation.id, m.sender.id) self.bot.send_admin_alert( 'Kicked {}: {} [{}] from group {} [{}] for {}: {}'.format( spam_type, name, m.sender.id, self.bot.groups[gid].title, gid, content, text)) self.bot.send_message(m, self.bot.trans.errors.idiot_kicked, extra={'format': 'HTML'}) self.bot.send_message(m, 'deleteMessage', 'system', extra={'message_id': m.id})
def run(self, m): uid = m.sender.id gid = m.conversation.id if gid > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if has_tag(self.bot, m.conversation.id, 'noroulette'): del_tag(self.bot, gid, 'roulette:?') return text = '' bullets = None roulette = has_tag(self.bot, gid, 'roulette:?', return_match=True) if roulette: bullets = int(roulette[0].split(':')[1]) if not bullets: set_tag(self.bot, gid, 'roulette:6') bullets = 6 if randint(1, bullets) == 1: del_tag(self.bot, gid, 'roulette:?') set_tag(self.bot, gid, 'roulette:6') if im_group_admin(self.bot, m): res = self.bot.bindings.kick_conversation_member( m.conversation.id, uid) if not res: text = self.bot.trans.plugins.russian_roulette.strings.cant_bang % m.sender.first_name else: text = self.bot.trans.plugins.russian_roulette.strings.bang % m.sender.first_name else: text = self.bot.trans.plugins.russian_roulette.strings.cant_bang % m.sender.first_name else: del_tag(self.bot, gid, 'roulette:%s' % bullets) bullets -= 1 set_tag(self.bot, gid, 'roulette:%s' % bullets) text = self.bot.trans.plugins.russian_roulette.strings.empty % ( m.sender.first_name, bullets) self.bot.send_message(m, text, extra={'format': 'HTML'})
def run(self, m): if has_tag(self.bot, m.conversation.id, 'noanimals'): return if is_command(self, 1, m.content): url = 'https://api.thecatapi.com/v1/images/search' params = { 'api_key': self.bot.config.api_keys.cat_api, 'format': 'src' } photo = send_request(url, params=params, parse=False, bot=self.bot) if photo: return self.bot.send_message(m, photo, 'photo') else: return self.bot.send_message(m, self.bot.trans.errors.connection_error) elif is_command(self, 2, m.content): url = 'https://dog.ceo/api/breeds/image/random' data = send_request(url, bot=self.bot) if data: return self.bot.send_message(m, data.message, 'photo') else: return self.bot.send_message(m, self.bot.trans.errors.connection_error)
def run(self, m): if has_tag(self.bot, m.conversation.id, 'nonsfw'): return self.bot.send_message( m, self.bot.trans.plugins.config.strings.disabled, extra={'format': 'HTML'}) nsfw = '-1001230470587' hentai = '-1001495126561' p**n = '-1001409180171' if is_command(self, 2, m.content): cid = hentai elif is_command(self, 3, m.content): cid = p**n else: cid = nsfw if self.bot.info.is_bot: info = self.bot.conversation_info(cid) if info: msg = None last = info['last_message']['id'] start = 1 retries = 100 while not msg: rid = randint(start, last) while rid in self.invalid_ids: rid = randint(start, last) msg = self.bot.get_message(cid, rid) if not msg and not rid in self.invalid_ids: retries -= 1 self.invalid_ids.append(rid) if retries <= 0: msg = self.bot.get_message(cid, last) return self.bot.forward_message(msg, m.conversation.id) return self.bot.forward_message(msg, m.conversation.id) else: history = self.bot.bindings.server_request( 'getChatHistory', { 'chat_id': int(cid), 'from_message_id': 0, 'offset': 0, 'limit': 100 }) if history: msg = None while not msg: index = randint(0, len(history['messages']) - 1) msg = self.bot.get_message( cid, history['messages'][index]['id']) return self.bot.forward_message(msg, m.conversation.id)
def run(self, m): if has_tag(self.bot, m.sender.id, 'noreactions') or has_tag(self.bot, m.conversation.id, 'noreactions'): return for reaction, attributes in self.bot.trans.plugins.reactions.strings.items(): for trigger in attributes: if compile(self.format_text(trigger, m), flags=IGNORECASE).search(m.content): text = self.format_text(reaction, m) types = ['photo', 'audio', 'document', 'voice', 'sticker', 'video'] for _type in types: if text.startswith(_type + ':'): content = text.split(':')[1] return self.bot.send_message(m, content, _type) return self.bot.send_message(m, text, extra={'format': 'Markdown'})
def run(self, m): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) input = get_input(m, ignore_reply=False) parameter = first_word(input) enabled = ['reactions', 'roulette', 'replies', 'pole', 'fiesta', 'nsfw'] disabled = ['antispam', 'antiarab', 'antirussian', 'polereset'] config = {} for param in enabled: config[param] = not has_tag( self.bot, m.conversation.id, 'no' + param) for param in disabled: config[param] = has_tag(self.bot, m.conversation.id, param) text = '' if not input: text = self.bot.trans.plugins.config.strings.explanation % "', '".join( config) for param in config: text += '\n' + ('✔️' if config[param] else '❌') + \ ' ' + self.bot.trans.plugins.config.strings[param] elif parameter in enabled or parameter in disabled: if not is_admin(self.bot, m.sender.id, m) and not is_trusted(self.bot, m.sender.id, m): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if config[parameter]: if parameter in enabled: set_tag(self.bot, m.conversation.id, 'no' + parameter) elif parameter in disabled: del_tag(self.bot, m.conversation.id, parameter) else: if parameter in enabled: del_tag(self.bot, m.conversation.id, 'no' + parameter) elif parameter in disabled: set_tag(self.bot, m.conversation.id, parameter) text = ('❌' if config[parameter] else '✔️') + \ ' ' + self.bot.trans.plugins.config.strings[parameter] return self.bot.send_message(m, text, extra={'format': 'HTML'})
def run(self, m): input = get_input(m) if not is_owner(self.bot, m.sender.id) and not is_trusted(self.bot, m.sender.id): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not input: return self.bot.send_message(m, generate_command_help(self, m.content), extra={'format': 'HTML'}) # return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) if not m.reply: input = all_but_first_word(input) target = get_target(self.bot, m, get_input(m)) if target: name = get_username(self.bot, target, False) elif first_word(input) == '-g': target = str(m.conversation.id) name = get_username(self.bot, target, False) else: target = str(m.sender.id) name = get_username(self.bot, target, False) tags = input.split() if not target: return self.bot.send_message(m, self.bot.trans.errors.no_results, extra={'format': 'HTML'}) # Adds a tag to user or group. # if is_command(self, 1, m.content): for tag in tags: if not has_tag(self.bot, target, tag): set_tag(self.bot, target, tag) return self.bot.send_message(m, self.bot.trans.plugins.tags.strings.tagged % (name, tags), extra={'format': 'HTML'}) # Removes a tag from user or group. # elif is_command(self, 2, m.content): for tag in tags: if has_tag(self.bot, target, tag): del_tag(self.bot, target, tag) return self.bot.send_message(m, self.bot.trans.plugins.tags.strings.untagged % (name, tags), extra={'format': 'HTML'})
def request_processing(self, request, response): leave_list = [ 'no rights', 'no write access', 'not enough rights to send', 'need administrator rights', 'channel_private' ] other_error = True for term in leave_list: if term in response['message'].lower() and not has_tag( self.bot, request['chat_id'], 'resend:?') and not has_tag( self.bot, request['chat_id'], 'fwd:?'): self.bot.send_admin_alert('Leaving chat: {} [{}]'.format( self.bot.groups[str(request['chat_id'])].title, request['chat_id'])) res = self.bot.bindings.kick_conversation_member( request['chat_id'], self.bot.info.id) other_error = False break if response['message'].lower() == 'invalid remote id': pins = self.bot.pins.copy() for pin in pins: if 'content' in self.bot.pins[pin] and 'type' in self.bot.pins[ pin] and self.bot.pins[pin]['type'] in request[ 'input_message_content'] and self.bot.pins[pin][ 'content'] == request['input_message_content'][ self.bot.pins[pin]['type']]['id']: if pin in self.bot.pins: self.bot.send_admin_alert( 'Deleting invalid pin: {} [{}]'.format( pin, self.bot.pins[pin]['content'])) delete_data('pins/%s/%s' % (self.bot.name, pin)) del self.bot.pins[pin] other_error = False elif response['message'].lower() == 'chat not found': logging.info('Chat not found: {}'.format(request['chat_id'])) other_error = False if other_error: self.bot.send_alert(request) self.bot.send_alert(response)
def run(self, m): if not is_trusted(self.bot, m.sender.id): return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'}) if not input: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) input = get_input(m) if m.reply: target = str(m.reply.sender.id) name = m.reply.sender.first_name elif first_word(input) == '-g': target = str(m.conversation.id) name = m.conversation.title input = all_but_first_word(input) elif first_word(input).isdigit(): target = first_word(input) name = target input = all_but_first_word(input) else: target = str(m.sender.id) name = m.sender.first_name tags = input.split() # Adds a tag to user or group. # if is_command(self, 1, m.content): for tag in tags: if not has_tag(self.bot, target, tag): set_tag(self.bot, target, tag) return self.bot.send_message(m, self.bot.trans.plugins.tags.strings.tagged % (name, tags), extra={'format': 'HTML'}) # Removes a tag from user or group. # elif is_command(self, 2, m.content): for tag in tags: if has_tag(self.bot, target, tag): del_tag(self.bot, target, tag) return self.bot.send_message(m, self.bot.trans.plugins.tags.strings.untagged % (name, tags), extra={'format': 'HTML'})
def always(self, m): id = str(m.conversation.id) if has_tag(self.bot, id, 'resend:?') or has_tag(self.bot, id, 'fwd:?'): for tag in self.bot.tags[id]: if tag.startswith('resend:'): cid = tag.split(':')[1] if m.type == 'photo' or m.type == 'video' or m.type == 'document' or (m.type == 'text' and 'urls' in m.extra): r = deepcopy(m) r.conversation.id = cid if 'urls' in r.extra: for url in r.extra['urls']: self.bot.send_message(r, url, extra={'preview': True}) else: self.bot.send_message(r, m.content, m.type, extra={'preview': True}) elif tag.startswith('fwd:'): cid = tag.split(':')[1] if m.type == 'photo' or m.type == 'document' or m.type == 'url': self.bot.forward_message(cid, m)
def request_processing(self, request, response): self.bot.send_alert(request) self.bot.send_alert(response) leave_list = ['no rights', 'no write access', 'not enough rights to send', 'need administrator rights', 'CHANNEL_PRIVATE'] for term in leave_list: if term in request.result and not has_tag(self.bot, request['chat_id'], 'resend:?') and not has_tag(self.bot, request['chat_id'], 'fwd:?'): self.bot.send_admin_alert('Leaving chat: {} [{}]'.format( self.bot.groups[str(request['chat_id'])].title, request['chat_id'])) res = self.bot.bindings.kick_conversation_member( request['chat_id'], self.bot.info.id) self.bot.send_admin_alert(res) break
def on_message_receive(self, msg): try: ignore_message = False if msg.content == None or (msg.type != 'inline_query' and msg.date < time() - 60 * 5): return # if msg.sender.id != self.config['owner'] and not is_trusted(self, msg.sender.id, msg) and (has_tag(self, msg.conversation.id, 'spam') or has_tag(self, msg.sender.id, 'spam')): # ignore_message = True # self.send_message(msg, self.trans.errors.spammer_detected, extra={'format': 'HTML'}) if msg.sender.id != self.config['owner'] and not is_trusted( self, msg.sender.id, msg) and (has_tag(self, msg.conversation.id, 'muted') or has_tag(self, msg.sender.id, 'muted')): ignore_message = True step = get_step(self, msg.conversation.id) if step: if not ignore_message: for plugin in self.plugins: if get_plugin_name(plugin) == step.plugin and hasattr( plugin, 'steps'): if msg.content.startswith('/cancel'): plugin.steps(msg, -1) cancel_steps(self, msg.conversation.id) if msg.content.startswith('/done'): plugin.steps(msg, 0) cancel_steps(self, msg.conversation.id) else: plugin.steps(msg, step['step']) else: for plugin in self.plugins: # Always do this action for every message. # if hasattr(plugin, 'always'): plugin.always(msg) # If no query show help # if msg.type == 'inline_query' and not ignore_message: if msg.content == '': msg.content = 'help' if hasattr(plugin, 'commands') and not ignore_message: # Check if any command of a plugin matches. # for command in plugin.commands: if 'parameters' not in command: command['parameters'] = None if 'command' in command: if self.check_trigger(command['command'], command['parameters'], msg, plugin): break if 'keep_default' in command and command[ 'keep_default']: if self.check_trigger( command['command'], command['parameters'], msg, plugin, False, True): break if 'friendly' in command and not has_tag( self, msg.sender.id, 'noreplies' ) and not has_tag( self, msg.conversation.id, 'noreplies' ) and msg.conversation.id != self.config.alerts_conversation_id and msg.conversation.id != self.config.admin_conversation_id: if self.check_trigger(command['friendly'], command['parameters'], msg, plugin, True): break if 'shortcut' in command: if self.check_trigger(command['shortcut'], command['parameters'], msg, plugin): break if 'keep_default' in command and command[ 'keep_default']: if self.check_trigger( command['shortcut'], command['parameters'], msg, plugin, False, True): break except KeyboardInterrupt: pass except Exception as e: catch_exception(e, self)
def run(self, m): if is_command(self, 1, m.content): username = get_input(m) if not username: #username = get_setting(self.bot, m.sender.id, 'lastfm.username') tags = has_tag(self.bot, m.sender.id, 'lastfm:?', return_match=True) if tags and len(tags) > 0: username = tags[0].split(':')[1] if not username and m.sender.username: username = m.sender.username if not username: return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) url = 'http://ws.audioscrobbler.com/2.0/' params = { 'method': 'user.getrecenttracks', 'format': 'json', 'limit': '1', 'api_key': self.bot.config.api_keys.lastfm, 'user': username } lastfm = send_request(url, params=params, bot=self.bot) # If the user didn't have any tracks or doesn't exist return No Results error. # try: last = lastfm.recenttracks.track[0] except: return self.bot.send_message(m, self.bot.trans.errors.no_results, extra={'format': 'HTML'}) artist = last.artist['#text'].title() track = last.name.title() album = last.album['#text'].title() track_url = last.url try: nowplaying = last['@attr'].nowplaying if nowplaying == 'true': nowplaying = True else: nowplaying == False except: date = last.date['#text'] nowplaying = False if nowplaying: text = self.bot.trans.plugins.lastfm.strings.now_playing % username else: text = self.bot.trans.plugins.lastfm.strings.last_played % username text += '\n🎵 <i>%s</i>\n💽 %s' % (track, artist) if album: text += ' - %s' % album # Gets the link of a Youtube video of the track. # url = 'https://www.googleapis.com/youtube/v3/search' params = { 'type': 'video', 'part': 'snippet', 'maxResults': '1', 'q': '%s %s' % (track, artist), 'key': self.bot.config.api_keys.google_developer_console } youtube = send_request(url, params=params, bot=self.bot) if not 'error' in youtube and len(youtube['items']) > 0: text += '\n\n🌐 %s\n%s\nhttps://youtu.be/%s' % ( self.bot.trans.plugins.lastfm.strings.might_be, youtube['items'][0].snippet.title, youtube['items'][0].id.videoId) self.bot.send_message( m, text, extra={'format': 'HTML', 'preview': False}) elif is_command(self, 2, m.content): input = get_input(m) if not input: return self.bot.send_message(m, generate_command_help(self, m.content), extra={'format': 'HTML'}) # return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'}) # set_setting(self.bot, m.sender.id, 'lastfm.username', input) del_tag(self.bot, m.sender.id, 'lastfm:?') set_tag(self.bot, m.sender.id, 'lastfm:%s' % input) self.bot.send_message(m, self.bot.trans.plugins.lastfm.strings.username_set, extra={ 'format': 'HTML', 'preview': False})
def run(self, m): input = get_input(m) text = None if m.reply: uid = str(m.reply.sender.id) else: uid = str(m.sender.id) # Get character data if is_command(self, 1, m.content): summoner_name = None if not input: tags = has_tag(self.bot, m.sender.id, 'lol:?', return_match=True) if tags and len(tags) > 0: summoner_info = tags[0].split(':')[1] if ('/' in summoner_info): self.region = self.regions[summoner_info.split('/')[0]] summoner_name = summoner_info.split('/')[1] if not summoner_name: return self.bot.send_message(m, generate_command_help( self, m.content), extra={'format': 'HTML'}) else: if first_word(input).lower() in self.regions: self.region = self.regions[first_word(input).lower()] summoner_name = all_but_first_word(input) else: self.region = self.regions['euw'] summoner_name = input summoner = self.summoner_by_name(summoner_name) if not summoner or 'status' in summoner and summoner['status'][ 'status_code'] != 200: return self.bot.send_message( m, self.bot.trans.errors.connection_error, extra={'format': 'HTML'}) account = self.account_by_puuid(summoner.puuid) masteries = self.champion_masteries(summoner.id) ranked = self.league_entries(summoner.id) if self.latest_version: icon_url = "http://ddragon.leagueoflegends.com/cdn/{}/img/profileicon/{}.png".format( self.latest_version, summoner.profileIconId) else: icon_url = None opgg_region = self.region[ 'platform'] if self.region['platform'] != 'kr' else 'www' opgg = 'http://{}.op.gg/summoner/userName={}'.format( opgg_region, ''.join(summoner_name.split())) text = '{} (Lv: {})\n'.format(summoner.name, summoner.summonerLevel) if account and 'gameName' in account: text += '{}\n'.format(account.gameName + '#' + account.tagLine) if masteries: text += '\nMasteries:' for mastery in masteries[:5]: text += '\n\t{}: Lv {} ({}k)'.format( self.championIds[str(mastery['championId'])], mastery['championLevel'], int(mastery['championPoints'] / 1000)) if ranked: for queue in ranked: text += '\n\n{}:\n\tLeague: {} {} ({}LP)'.format( self.ranked_queueType(queue['queueType']), self.ranked_tier(queue['tier']), queue['rank'], queue['leaguePoints']) text += '\n\tWins/Losses: {} / {} ({}%)'.format( queue['wins'], queue['losses'], str( int((float(queue['wins']) / (float(queue['wins']) + float(queue['losses']))) * 100)).replace( '.', '\'')) # text += '\n\n<a href="{}">{}</a>'.format(opgg, 'OP.GG') if icon_url: return self.bot.send_message(m, icon_url, 'photo', extra={ 'caption': text, 'format': 'HTML', 'preview': True }) return self.bot.send_message(m, text, extra={ 'format': 'HTML', 'preview': True })
def run(self, m): if str(m.sender.id).startswith('-100'): return if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if has_tag(self.bot, m.conversation.id, 'nopole'): return gid = str(m.conversation.id) uid = m.sender.id now = datetime.now().replace(microsecond=0) date = now.isoformat().split('T')[0] text = None # Pole ranking if is_command(self, 1, m.content): if time_in_range(time(1, 0, 0), time(2, 0, 0), now.time()): type = 1 elif time_in_range(time(12, 0, 0), time(13, 0, 0), now.time()): type = 2 else: type = 0 if gid in self.bot.poles: ranking = DictObject() for day in self.bot.poles[gid]: if type == 0: if 'pole' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].pole)].p += 1 except: ranking[str(self.bot.poles[gid][day].pole)] = { 'p': 1, 's': 0, 'f': 0, 'i': 0 } if 'subpole' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].subpole)].s += 1 except: ranking[str( self.bot.poles[gid][day].subpole)] = { 'p': 0, 's': 1, 'f': 0, 'i': 0 } if 'fail' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].fail)].f += 1 except: ranking[str(self.bot.poles[gid][day].fail)] = { 'p': 0, 's': 0, 'f': 1, 'i': 0 } if 'iron' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].iron)].i += 1 except: ranking[str(self.bot.poles[gid][day].iron)] = { 'p': 0, 's': 0, 'f': 0, 'i': 1 } if type == 1: if 'canaria' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].canaria)].c += 1 except: ranking[str( self.bot.poles[gid][day].canaria)] = { 'c': 1 } if type == 2: if 'andaluza' in self.bot.poles[gid][day]: try: ranking[str( self.bot.poles[gid][day].andaluza)].a += 1 except: ranking[str( self.bot.poles[gid][day].andaluza)] = { 'a': 1 } text = '' if type == 0: text = self.bot.trans.plugins.pole.strings.ranking for uid, values in self.order_by_points(ranking, type): points = str((values.p * 3) + (values.s * 1) + (values.f * 0.5) + (values.i * 0.1)).rstrip('0').rstrip('.') text += '\n ' + self.bot.trans.plugins.pole.strings.ranking_points % ( self.bot.users[uid].first_name, points) poles = '\n\n' + self.bot.trans.plugins.pole.strings.poles poles_empty = True for uid, values in self.order_by_poles(ranking): if values.p: poles_empty = False poles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_poles % ( self.bot.users[uid].first_name, values.p) if not poles_empty: text += poles subpoles = '\n\n' + self.bot.trans.plugins.pole.strings.subpoles subpoles_empty = True for uid, values in self.order_by_subpoles(ranking): if values.s: subpoles_empty = False subpoles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_subpoles % ( self.bot.users[uid].first_name, values.s) if not subpoles_empty: text += subpoles fails = '\n\n' + self.bot.trans.plugins.pole.strings.fails fails_empty = True for uid, values in self.order_by_fails(ranking): if values.f: fails_empty = False fails += '\n ' + self.bot.trans.plugins.pole.strings.ranking_fails % ( self.bot.users[uid].first_name, values.f) if not fails_empty: text += fails irons = '\n\n' + self.bot.trans.plugins.pole.strings.irons irons_empty = True for uid, values in self.order_by_irons(ranking): if values.i: irons_empty = False irons += '\n ' + self.bot.trans.plugins.pole.strings.ranking_irons % ( self.bot.users[uid].first_name, values.i) if not irons_empty: text += irons elif type == 1: poles_canarias = '\n\n' + self.bot.trans.plugins.pole.strings.poles_canarias poles_canarias_empty = True for uid, values in self.order_by_poles_canarias(ranking): if values.c: poles_canarias_empty = False poles_canarias += '\n ' + \ self.bot.trans.plugins.pole.strings.ranking_poles % ( self.bot.users[uid].first_name, values.c) if not poles_canarias_empty: text += poles_canarias elif type == 2: poles_andaluzas = '\n\n' + self.bot.trans.plugins.pole.strings.poles_andaluzas poles_andaluzas_empty = True for uid, values in self.order_by_poles_andaluzas(ranking): if values.a: poles_andaluzas_empty = False poles_andaluzas += '\n ' + \ self.bot.trans.plugins.pole.strings.ranking_poles % ( self.bot.users[uid].first_name, values.a) if not poles_andaluzas_empty: text += poles_andaluzas # Pole elif is_command(self, 2, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'pole': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date]: self.bot.poles[gid][date].pole = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole % user # Subole elif is_command(self, 3, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'subpole': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'subpole' in self.bot.poles[gid][date]: self.bot.poles[gid][date].subpole = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_subpole % user # Fail elif is_command(self, 4, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'fail': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][ date] or not 'subpole' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'fail' in self.bot.poles[gid][date]: self.bot.poles[gid][date].fail = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_fail % user # Pole canaria elif is_command(self, 5, m.content): if self.has_pole(gid, uid, date, 1): return if not time_in_range(time(1, 0, 0), time(2, 0, 0), now.time()): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'canaria': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'canaria' in self.bot.poles[gid][date]: self.bot.poles[gid][date].canaria = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole_canaria % user # Pole andaluza elif is_command(self, 6, m.content): if self.has_pole(gid, uid, date, 1): return if not time_in_range(time(12, 0, 0), time(13, 0, 0), now.time()): uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon_for_andaluza % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'andaluza': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'andaluza' in self.bot.poles[gid][date]: self.bot.poles[gid][date].andaluza = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole_andaluza % user # Hierro elif is_command(self, 7, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({date: {'iron': uid}}) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][ date] or not 'subpole' in self.bot.poles[gid][ date] or not 'fail' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[ uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'iron' in self.bot.poles[gid][date]: self.bot.poles[gid][date].iron = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = '' if 'first_name' in self.bot.users[uid]: user += self.bot.users[uid]['first_name'] if 'username' in self.bot.users[uid] and self.bot.users[ uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_iron % user # Pole reset elif is_command(self, 8, m.content): if has_tag(self.bot, m.conversation.id, 'polereset'): if is_trusted(self.bot, m.sender.id, m): delete_data('poles/%s/%s' % (self.bot.name, gid)) self.bot.poles.pop(gid, None) text = self.bot.trans.plugins.pole.strings.polereset_done else: text = self.bot.trans.errors.admin_required else: text = self.bot.trans.plugins.config.strings.disabled if text: self.bot.send_message(m, text, extra={'format': 'HTML'})
def on_message_receive(self, msg): try: if msg.content == None or (msg.type != 'inline_query' and msg.date < time() - 60): return if msg.sender.id != self.config['owner'] and ( has_tag(self, msg.conversation.id, 'muted') or has_tag(self, msg.sender.id, 'muted')): return step = get_step(self, msg.conversation.id) if step: for plugin in self.plugins: if get_plugin_name(plugin) == step.plugin and hasattr( plugin, 'steps'): if msg.content.startswith('/cancel'): plugin.steps(msg, -1) cancel_steps(self, msg.conversation.id) if msg.content.startswith('/done'): plugin.steps(msg, 0) cancel_steps(self, msg.conversation.id) else: plugin.steps(msg, step['step']) else: for plugin in self.plugins: # Always do this action for every message. # if hasattr(plugin, 'always'): plugin.always(msg) # If no query show help # if msg.type == 'inline_query': if msg.content == '': msg.content = 'help' # Check if any command of a plugin matches. # for command in plugin.commands: if 'parameters' not in command: command['parameters'] = None if 'command' in command: if self.check_trigger(command['command'], command['parameters'], msg, plugin): break if 'friendly' in command: if self.check_trigger(command['friendly'], command['parameters'], msg, plugin, True): break if 'shortcut' in command: if self.check_trigger(command['shortcut'], command['parameters'], msg, plugin): break except KeyboardInterrupt: pass except Exception as e: catch_exception(e, self)
def run(self, m): if m.conversation.id > 0: return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'}) if has_tag(self.bot, m.conversation.id, 'nopole'): return gid = str(m.conversation.id) uid = m.sender.id date = datetime.now().replace(microsecond=0).isoformat().split('T')[0] # Pole ranking if is_command(self, 1, m.content): if gid in self.bot.poles: ranking = DictObject() for day in self.bot.poles[gid]: if 'pole' in self.bot.poles[gid][day]: try: ranking[str(self.bot.poles[gid][day].pole)].p += 1 except: ranking[str(self.bot.poles[gid][day].pole)] = { 'p': 1, 's': 0, 'f': 0 } if 'subpole' in self.bot.poles[gid][day]: try: ranking[str(self.bot.poles[gid][day].subpole)].s += 1 except: ranking[str(self.bot.poles[gid][day].subpole)] = { 'p': 0, 's': 1, 'f': 0 } if 'fail' in self.bot.poles[gid][day]: try: ranking[str(self.bot.poles[gid][day].fail)].f += 1 except: ranking[str(self.bot.poles[gid][day].fail)] = { 'p': 0, 's': 0, 'f': 1 } text = self.bot.trans.plugins.pole.strings.ranking for uid, values in self.order_by_points(ranking): points = str((values.p * 3) + (values.s * 1) + (values.f * 0.5)).rstrip('0').rstrip('.') text += '\n ' + self.bot.trans.plugins.pole.strings.ranking_points % (self.bot.users[uid].first_name, points) poles = '\n\n' + self.bot.trans.plugins.pole.strings.poles poles_empty = True for uid, values in self.order_by_poles(ranking): if values.p: poles_empty = False poles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_poles % (self.bot.users[uid].first_name, values.p) if not poles_empty: text += poles subpoles = '\n\n' + self.bot.trans.plugins.pole.strings.subpoles subpoles_empty = True for uid, values in self.order_by_subpoles(ranking): if values.s: subpoles_empty = False subpoles += '\n ' + self.bot.trans.plugins.pole.strings.ranking_subpoles % (self.bot.users[uid].first_name, values.s) if not subpoles_empty: text += subpoles fails = '\n\n' + self.bot.trans.plugins.pole.strings.fails fails_empty = True for uid, values in self.order_by_fails(ranking): if values.f: fails_empty = False fails += '\n ' + self.bot.trans.plugins.pole.strings.ranking_fails % (self.bot.users[uid].first_name, values.f) if not fails_empty: text += fails # Pole elif is_command(self, 2, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({ date: { 'pole': uid } }) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date]: self.bot.poles[gid][date].pole = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = self.bot.users[uid].first_name if 'username' in self.bot.users[uid] and self.bot.users[uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_pole % user # Subole elif is_command(self, 3, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({ date: { 'subpole': uid } }) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'subpole' in self.bot.poles[gid][date]: self.bot.poles[gid][date].subpole = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = self.bot.users[uid].first_name if 'username' in self.bot.users[uid] and self.bot.users[uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_subpole % user # Fail elif is_command(self, 4, m.content): if self.has_pole(gid, uid, date): return if not gid in self.bot.poles: self.bot.poles[gid] = DictObject({ date: { 'fail': uid } }) else: if not date in self.bot.poles[gid]: self.bot.poles[gid][date] = DictObject() if not 'pole' in self.bot.poles[gid][date] or not 'subpole' in self.bot.poles[gid][date]: uid = str(uid) text = self.bot.trans.plugins.pole.strings.too_soon % self.bot.users[uid].first_name return self.bot.send_message(m, text, extra={'format': 'HTML'}) elif not 'fail' in self.bot.poles[gid][date]: self.bot.poles[gid][date].fail = uid else: return set_data('poles/%s/%s/%s' % (self.bot.name, gid, date), self.bot.poles[gid][date]) uid = str(uid) user = self.bot.users[uid].first_name if 'username' in self.bot.users[uid] and self.bot.users[uid].username: user = '******' + self.bot.users[uid].username text = self.bot.trans.plugins.pole.strings.got_fail % user if text: self.bot.send_message(m, text, extra={'format': 'HTML'})
def always(self, m): gid = str(m.conversation.id) if m.sender.is_bot: logging.info('ignoring bot: {} [{}]'.format( m.sender.first_name, m.sender.id)) return if m.sender.id == 777000: logging.info('ignoring anonymous message: {} [{}]'.format( m.sender.first_name, m.sender.id)) return if has_tag(self.bot, m.sender.id, 'muted'): logging.info('ignoring muted user: {} [{}]'.format( m.sender.first_name, m.sender.id)) return if 'reply_markup' in m.extra: logging.info('ignoring reply markup: {} [{}]'.format( m.sender.first_name, m.sender.id)) return if 'via_bot_user_id' in m.extra: uid = str(m.extra['via_bot_user_id']) name = None if uid in self.bot.users: name = self.bot.users[uid].first_name else: info = self.bot.bindings.server_request( 'getUser', {'user_id': int(uid)}) name = info['first_name'] self.bot.users[uid] = { 'first_name': info['first_name'], 'last_name': info['last_name'], 'messages': 0 } set_data('users/%s/%s' % (self.bot.name, uid), self.bot.users[uid]) logging.info('ignoring message via bot: {} [{}]'.format( name, m.extra['via_bot_user_id'])) return if has_tag(self.bot, gid, 'resend:?') or has_tag( self.bot, gid, 'fwd:?'): for tag in self.bot.tags[gid]: forward = False if tag.startswith('resend:') or tag.startswith('fwd:'): cid = int(tag.split(':')[1]) if 'from_chat_id' in m.extra: if str(m.extra['from_chat_id']) == str(cid): break elif str(m.extra['from_chat_id']) != '0': if has_tag(self.bot, cid, 'resend:?') or has_tag( self.bot, cid, 'fwd:?'): logging.info('forward') forward = True logging.info('tag: {}, forward: {}'.format(tag, forward)) if tag.startswith('resend:') and not forward: cid = int(tag.split(':')[1]) if m.type == 'photo' or m.type == 'video' or m.type == 'animation' or m.type == 'document' or ( m.type == 'text' and 'urls' in m.extra): r = deepcopy(m) r.conversation.id = cid r.conversation.title = tag if 'urls' in r.extra: for url in r.extra['urls']: input_match = compile( r'(?i)(?:t|telegram|tlgrm)\.(?:me|dog)\/joinchat\/([a-zA-Z0-9\-]+)', flags=IGNORECASE).search(url) if input_match and input_match.group( 1) or 'joinchat/' in url: logging.info( 'ignoring telegram url: {}'.format( fix_telegram_link(url))) else: if 'instagram' in url: url = url.split('?')[0] self.bot.send_message( r, url, extra={'preview': True}) else: self.bot.send_message(r, m.content, m.type, extra={'preview': True}) elif m.type != 'text': logging.info('invalid type: %s' % m.type) elif tag.startswith('fwd:') or forward: cid = int(tag.split(':')[1]) if m.type == 'photo' or m.type == 'document' or m.type == 'url': self.bot.forward_message(m, cid) if has_tag(self.bot, gid, 'discord:?'): for tag in self.bot.tags[gid]: if tag.startswith('discord:'): token = tag.split(':')[1] webhook_url = 'https://discord.com/api/webhooks/{}'.format( token) if m.type == 'photo' or m.type == 'video' or m.type == 'animation' or m.type == 'document' or ( m.type == 'text' and 'urls' in m.extra): if 'urls' in m.extra: for url in m.extra['urls']: input_match = compile( r'(?i)(?:t|telegram|tlgrm)\.(?:me|dog)\/joinchat\/([a-zA-Z0-9\-]+)', flags=IGNORECASE).search(url) if input_match and input_match.group( 1) or 'joinchat/' in url: logging.info( 'ignoring telegram url: {}'.format( fix_telegram_link(url))) else: if 'instagram' in url: url = url.split('?')[0] send_request(webhook_url, {'content': url}, post=True) else: if m.content.startswith('http'): send_request(webhook_url, {'content': m.content}, post=True) else: file = self.bot.get_file(m.content) if file: send_request( webhook_url, post=True, files={'file': open(file, 'rb')}) elif m.type != 'text': logging.info('invalid type: %s' % m.type)