Beispiel #1
0
    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')
Beispiel #2
0
    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'})
Beispiel #3
0
    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'})

        input = get_input(m)
        text = self.bot.trans.errors.no_results

        # Shutdown
        if is_command(self, 1, m.content):
            self.bot.stop()
            text = self.bot.trans.plugins.core.strings.shutting_down

        # Reload plugins
        elif is_command(self, 2, m.content):
            self.plugins = self.init_plugins()
            text = self.bot.trans.plugins.core.strings.reloading_plugins

        # Reload database
        elif is_command(self, 3, m.content):
            self.bot.get_database()
            text = self.bot.trans.plugins.core.strings.reloading_database

        # Send messages
        elif is_command(self, 4, m.content):
            text = self.bot.trans.errors.not_implemented

        # Run shell commands
        elif is_command(self, 5, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})
            return self.bot.send_message(m, '<code>%s</code>' % (subprocess.getoutput(input)), extra={'format': 'HTML'})

        # Run python code
        elif is_command(self, 6, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            cout = StringIO()
            sys.stdout = cout
            cerr = StringIO()
            sys.stderr = cerr

            exec(input)

            if cout.getvalue():
                return self.bot.send_message(m, '<code>%s</code>' % str(cout.getvalue()), extra={'format': 'HTML'})

            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

        if text:
            self.bot.send_message(m, text, extra={'format': 'HTML'})
Beispiel #4
0
    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'})
Beispiel #5
0
    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'})
Beispiel #6
0
    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)
Beispiel #7
0
    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'})
Beispiel #8
0
    def run(self, m):
        input = get_input(m)
        uid = str(m.sender.id)
        gid = str(m.conversation.id)

        # List all administration commands. #
        if is_command(self, 1, m.content):
            text = self.bot.trans.plugins.administration.strings.commands
            for command in self.commands:
                # Adds the command and parameters#
                if self.commands[-1]['command'].replace('/', self.bot.config.prefix) in m.content:
                    text += '\n' + command['command'].lstrip('/')

                    if 'description' in command:
                        text += ' - %s' % command['description']
                    else:
                        text += ' - ?¿'
                else:
                    text += '\n • ' + command['command'].replace('/', self.bot.config.prefix)
                    if 'parameters' in command:
                        for parameter in command['parameters']:
                            name, required = list(parameter.items())[0]
                            # Bold for required parameters, and italic for optional #
                            if required:
                                text += ' <b>&lt;%s&gt;</b>' % name
                            else:
                                text += ' [%s]' % name

                    if 'description' in command:
                        text += '\n   <i>%s</i>' % command['description']
                    else:
                        text += '\n   <i>?¿</i>'
                        text += '\n   <i>?¿</i>'

            return self.bot.send_message(m, text, extra={'format': 'HTML'})

        # List all groups. #
        if is_command(self, 2, m.content):
            text = self.bot.trans.plugins.administration.strings.groups
            for gid, attr in self.administration.items():
                text += '\n • %s |%s|' % (self.groups[gid]['title'], attr['alias'])
            return self.bot.send_message(m, text, extra={'format': 'HTML'})

        # Join a group. #
        elif is_command(self, 3, m.content):
            if not input:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            for id in self.administration:
                if (input in self.administration or
                    input in self.administration[id]['alias'] or
                    input in self.groups[id]['title']):
                    gid_to_join = id
                    break

            if not gid_to_join in self.administration:
                return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            text = '<b>%s</b>\n<i>%s</i>\n\n%s' % (self.groups[gid_to_join]['title'], self.administration[gid_to_join]['description'], self.bot.trans.plugins.administration.strings.rules)
            i = 1
            for rule in self.administration[gid_to_join]['rules']:
                text += '\n %s. <i>%s</i>' % (i, rule)
                i += 1
            if not self.administration[gid_to_join]['rules']:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            if not self.administration[gid_to_join]['link']:
                text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink
            else:
                text += '\n\n<a href="%s">%s</a>' % (self.administration[gid_to_join]['link'], self.bot.trans.plugins.administration.strings.join)
            return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False})

        # Information about a group. #
        elif is_command(self, 4, m.content) or is_command(self, 9, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})
            if not gid in self.administration:
                if is_command(self, 4, m.content):
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})
                elif is_command(self, 9, m.content):
                    return

            text = '<b>%s</b>\n<i>%s</i>\n\n%s' % (self.groups[gid]['title'], self.administration[gid]['description'], self.bot.trans.plugins.administration.strings.rules)
            i = 1
            for rule in self.administration[gid]['rules']:
                text += '\n %s. <i>%s</i>' % (i, rule)
                i += 1

            if not self.administration[gid]['rules']:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            if is_command(self, 4, m.content):
                if not self.administration[gid]['link']:
                    text += '\n\n%s' % self.bot.trans.plugins.administration.strings.nolink
                else:
                    text += '\n\n<a href="%s">%s</a>' % (self.bot.trans.plugins.administration.strings.join. self.administration[gid]['link'])
            return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False})

        # Rules of a group. #
        elif is_command(self, 5, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})
            if not gid in self.administration:
                return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            if input and is_int(input):
                try:
                    i = int(input)
                    text = '%s. <i>%s</i>' % (i, self.administration[gid]['rules'][i - 1])
                except:
                    text = self.bot.trans.plugins.administration.strings.notfound

            else:
                text = self.bot.trans.plugins.administration.strings.rules
                i = 1
                for rule in self.administration[gid]['rules']:
                    text += '\n %s. <i>%s</i>' % (i, rule)
                    i += 1

            if not self.administration[gid]['rules']:
                text += '\n%s' % self.bot.trans.plugins.administration.strings.norules
            return self.bot.send_message(m, text, extra={'format': 'HTML', 'preview': False})

        # Kicks a user. #
        elif is_command(self, 6, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.id):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            if not input and not m.reply:
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            if m.reply:
                target = m.reply.sender.id
            elif input:
                target = input
            else:
                target = m.sender.id

            res = self.bot.kick_user(m, target)
            self.bot.unban_user(m, target)
            if res is None:
                return self.bot.send_message(m, self.bot.trans.errors.admin_required, extra={'format': 'HTML'})
            elif not res:
                return self.bot.send_message(m, self.bot.trans.errors.failed, extra={'format': 'HTML'})
            else:
                return self.bot.send_message(m, '<pre>An enemy has been slain.</pre>', extra={'format': 'HTML'})

            return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'})

        # Bans a user. #
        elif is_command(self, 7, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

            if not is_mod(self.bot, m.sender.id, m.conversation.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'})

            return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'})

        # Configures a group. #
        elif is_command(self, 8, m.content):
            if m.conversation.id > 0:
                return self.bot.send_message(m, self.bot.trans.errors.group_only, extra={'format': 'HTML'})

            if not is_trusted(self.bot, m.sender.id):
                return self.bot.send_message(m, self.bot.trans.errors.permission_required, extra={'format': 'HTML'})

            parameters = [
                'add',
                'remove',
                'alias',
                'description',
                'rules',
                'rule'
            ]

            if not input:
                text = '<b>Available commands:</b>'
                for param in parameters:
                    text += '\n • %scfg %s' % (self.bot.config.prefix, param)
                return self.bot.send_message(m, self.bot.trans.errors.missing_parameter, extra={'format': 'HTML'})

            if first_word(input) == 'add':
                if not gid in self.administration:
                    self.administration[gid] = {
                        'alias': None,
                        'description': None,
                        'link': None,
                        'rules': []
                    }
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.added % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.already_added % m.conversation.title, extra={'format': 'HTML'})

            elif first_word(input) == 'remove':
                if gid in self.administration:
                    del(self.administration[gid])
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.removed % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            elif first_word(input) == 'alias':
                if gid in self.administration:
                    self.administration[gid]['alias'] = all_but_first_word(input)
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            elif first_word(input) == 'description':
                if gid in self.administration:
                    self.administration[gid]['description'] = all_but_first_word(input)
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            elif first_word(input) == 'link':
                if gid in self.administration:
                    self.administration[gid]['link'] = all_but_first_word(input)
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            elif first_word(input) == 'rules':
                if gid in self.administration:
                    self.administration[gid]['rules'] = all_but_first_word(input).split('\n')[0:]
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})

            elif first_word(input) == 'rule':
                if gid in self.administration:
                    try:
                        i = int(first_word(all_but_first_word(input)))-1
                        if i > len(self.administration[gid]['rules']):
                            i = len(self.administration[gid]['rules'])
                        elif i < 1:
                            i = 0
                    except:
                        return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'})
                    self.administration[gid]['rules'].insert(i, all_but_first_word(all_but_first_word(input)))
                    self.administration.store_database()
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.set % m.conversation.title, extra={'format': 'HTML'})
                else:
                    return self.bot.send_message(m, self.bot.trans.plugins.administration.strings.not_added % m.conversation.title, extra={'format': 'HTML'})


            else:
                return self.bot.send_message(m, self.bot.trans.errors.no_results, extra={'format': 'HTML'})

            return self.bot.send_message(m, self.bot.trans.errors.unknown, extra={'format': 'HTML'})
Beispiel #9
0
    def run(self, 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'})

        input = get_input(m)
        text = self.bot.trans.errors.no_results

        # Shutdown
        if is_command(self, 1, m.content):
            self.bot.stop()
            text = self.bot.trans.plugins.core.strings.shutting_down

        # Reload plugins
        elif is_command(self, 2, m.content):
            self.bot.plugins = self.bot.init_plugins()
            text = self.bot.trans.plugins.core.strings.reloading_plugins

        # Reload database
        elif is_command(self, 3, m.content):
            self.bot.get_database()
            text = self.bot.trans.plugins.core.strings.reloading_database

        # Send messages
        elif is_command(self, 4, m.content):
            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'})
            target = get_target(self.bot, m, input)
            message = all_but_first_word(input)
            r = deepcopy(m)
            r.conversation.id = target
            return self.bot.send_message(r, message)

        # Run shell commands
        elif is_command(self, 5, m.content):
            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'})
            code = '$ %s\n\n%s' % (input, subprocess.getoutput(input))
            return self.bot.send_message(
                m,
                '<code class="language-shell">%s</code>' % code,
                extra={'format': 'HTML'})

        # Run python code
        elif is_command(self, 6, m.content):
            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'})

            cout = StringIO()
            sys.stdout = cout
            cerr = StringIO()
            sys.stderr = cerr

            exec(input)

            code = None
            if cout.getvalue():
                code = '>>>%s\n\n%s' % (input, cout.getvalue())

            if cerr.getvalue():
                code = '>>>%s\n\n%s' % (input, cerr.getvalue())

            if code:
                return self.bot.send_message(
                    m,
                    '<code class="language-python">%s</code>' % code,
                    extra={'format': 'HTML'})

            sys.stdout = sys.__stdout__
            sys.stderr = sys.__stderr__

        # Change name
        elif is_command(self, 7, m.content):
            if not input:
                return self.bot.send_message(m,
                                             generate_command_help(
                                                 self, m.content),
                                             extra={'format': 'HTML'})
            ok = self.bot.bindings.server_request('setName', {
                'first_name': input,
                'last_name': '☆'
            })

            if not ok:
                text = self.bot.trans.errors.failed

        # Change bio
        elif is_command(self, 8, m.content):
            if not input:
                return self.bot.send_message(m,
                                             generate_command_help(
                                                 self, m.content),
                                             extra={'format': 'HTML'})
            ok = self.bot.bindings.server_request('setBio', {'bio': input})

            if not ok:
                text = self.bot.trans.errors.failed

        # Change photo
        elif is_command(self, 9, m.content):
            ok = False
            if m.reply and m.reply.type == 'photo':
                photo = self.bot.bindings.get_file(m.reply.content)
                if photo:
                    ok = self.bot.bindings.server_request(
                        'setProfilePhoto',
                        {'photo': self.bot.bindings.get_input_file(photo)})
                else:
                    ok = self.bot.bindings.server_request(
                        'setProfilePhoto', {
                            'photo':
                            self.bot.bindings.get_input_file(m.reply.content)
                        })

            if not ok:
                text = self.bot.trans.errors.failed

        # Create webhook
        elif is_command(self, 10,
                        m.content) and self.bot.config.bindings == 'discord':
            if not input:
                return self.bot.send_message(m,
                                             generate_command_help(
                                                 self, m.content),
                                             extra={'format': 'HTML'})
            self.bot.bindings.create_webhook(m.conversation.id, input)
            text = 'Created webhook: "{}"'.format(input)

        if text:
            self.bot.send_message(m, text, extra={'format': 'HTML'})