Beispiel #1
0
 def unpack_data(cls, data):
     unpacked = {}
     for (key, default) in cls.defaults.items():
         value = default
         if key in data and data[key] != None:
             v = data[key]
             if is_number(default):
                 value = int(v)
             elif isinstance(default, str):
                 value = str(v)
             elif isinstance(default, bool):
                 value = text_to_bool(v) if isinstance(v, str) else bool(v)
             elif isinstance(default, list) and isinstance(v, str):
                 if key in ['role_restriction', 'access_all_rooms_role']:
                     value = str_to_ids(v)
                 else:
                     value = re.split('[,]+', v)
         unpacked[key] = value
     return unpacked
Beispiel #2
0
    def set(self, ctx, field, value):
        result = (True, None)
        parsed_value = value
        if field not in self.defaults.keys():
            return (False, self.get_text('require_flags'))
        default = self.defaults[field]

        if field in ['voice_creation_channel', 'creation_channel']:
            parsed_value = 0
            channels = ctx.guild.voice_channels
            for c in channels:
                if len(value) > 1 and value in c.name or value == str(c.id):
                    parsed_value = c.id
        elif is_number(default):
            try:
                parsed_value = int(value)
            except ValueError:
                parsed_value = -1

            if field in ['size', 'default_size']:
                parsed_value = clamp(parsed_value, 2, 999)
            elif field == 'bitrate':
                parsed_value = clamp(parsed_value, 8, int(ctx.guild.bitrate_limit/1000))
            elif field == 'timeout':
                parsed_value = clamp(parsed_value, -1, 999)

        elif isinstance(default, bool):
            parsed_value = text_to_bool(value)

        elif isinstance(default, list):
            if field in ['role_restriction', 'access_all_rooms_role']:
                roles = []
                for word in re.split('[,\s]+', value):
                    try:
                        r = re.search(r'\d+', word)
                        if r:
                            role_id = int(r.group())
                            roles.append(role_id)
                    except ValueError:
                        result = (False, self.get_text('should_use_mentions'))
                parsed_value = ids_to_str(roles) 
            elif field == 'default_colors':
                colors = []
                for s in re.split('[,\s]+', value):
                    color = get_color(s, return_default=False)
                    if color:
                        colors.append(str(color.value))
                parsed_value = ','.join(colors)
            elif field == 'allowed_host_commands':
                commands = []
                for c in re.split('[,\s]+', value):
                    if c in default and c not in commands:
                        commands.append(c)
                if commands == []:
                    commands = default
                parsed_value = ','.join(commands)
            else:
                messages = []
                for s in re.split('[,]+', value):
                    m = s.strip().replace('__', '{0}')
                    if len(m) > 0:
                        messages.append(m)
                parsed_value = ','.join(messages)

        elif isinstance(default, str):
            if field == 'prefix':
                max_char_length = 5
                if len(value) > max_char_length:
                    result = (False, self.get_text('prefix_too_long').format(max_char_length))
            elif field == 'language':
                parsed_value = value[:2].lower()
                if parsed_value not in langs:
                    result = (False, self.get_text('language_not_exist').format(parsed_value))
            elif field == 'category_name':
                parsed_value = value[:99]

        (success, message) = result
        if (success):
            is_string = isinstance(parsed_value, str)
            text_value = parsed_value if not is_string or (is_string and len(parsed_value) > 0) else 'None'
            self.update(field, parsed_value)
            result = (True, self.get_text('set_value').format(field, text_value))
        return result